Issue to translate a query from SQL to Appian

Hello, 

I have a problem creating a query in Appian, which was previously created in SQL.

Basically, we have 2 tables, the main one with the CASE numbers and the status, and another one where we record the historical movements of those cases:

Main Table : CASE
IDCase Status
1 Active
2 Active
3 Active
4 Close
5 Active

Secondary Table: HistoryCase
IDHISTORY IDCASE LogText CreatedDT
1 1 Created 11/23/2024 10:34
2 1 Updated 11/23/2024 10:34
3 1 Assigned 11/23/2024 10:34
4 1 Updated 11/23/2024 10:34
5 1 Closed 11/23/2024 10:34
6 2 Created 11/23/2024 10:34
7 2 Updated 11/23/2024 10:34
8 2 Assigned 11/23/2024 10:34
9 2 Updated 11/23/2024 10:34
10 2 Updated 11/23/2024 10:34
11 2 Updated 11/23/2024 10:34
12 2 Closed 11/23/2024 10:34
13 3 Created 11/23/2024 10:34
14 3 Updated 11/23/2024 10:34
15 3 Closed 11/23/2024 10:34

Our SQL query has subqueries, where we look for the last "Update" of each "CASE", so one we have the latest we applied another filter for a range dates:

Select *
from
(
    SELECT IDCase, CONVERT(Date,MAX(CreatedDT)) as 'CreatedDT'
    from
    (
        select A.IDCase as 'Case', A.CreatedDT as 'CreatedDT', A.LogText as 'LogText', A.IDHISTORY as 'IDHISTORY', B.Status as 'Status',
        
        FROM [dbo].[HistoryCase] as A
        join [dbo].[CASE] as B on A.IDCase=B.IDCase
        
        where A.IDCase IS NOT NULL and A.LogText like 'Updated%' and B.Status <> 'Close'
    
    ) as Result
    group by IDCase

) AS FinalResult

where CreatedDT >= CONVERT(datetime,'2024-11-01') and CreatedDT <= CONVERT(datetime,'2024-11-30')
order by IDCase

This is how we translated in Appian:

a!queryRecordType(
          recordType: 'recordType!HistoryCase',
          fields: a!aggregationFields(
            groupings: {
              a!grouping(
                field: 'recordType!HistoryCase.IDCase',
                alias: "IDCaseGroup"
              )
            },
            measures: {
              a!measure(
                function: "MAX",
                field: 'recordType!HistoryCase.CreatedDT',
                alias: "CreatedDT_max_measure"
              )
            }
          ),
          filters: a!queryLogicalExpression(
          operator: "AND",
          logicalExpressions: a!queryLogicalExpression(
            operator: "AND",
            filters: {
              a!queryFilter(
                field: 'recordType!HistoryCase.CreatedDT',
                operator: "between",
                value: {
                  todatetime(ri!fromCreatedDT),
                  todatetime(ri!toCreatedDT) + 1
                }
              )
            },
            ignoreFiltersWithEmptyValues: true
          ),
          filters: {
            a!queryFilter(
              field: 'recordType!HistoryCase.LogText',
              operator: "starts with",
              value: "Updated"
            )
          },
          ignoreFiltersWithEmptyValues: true
        )
          pagingInfo: a!pagingInfo(
            startIndex: 1,
            batchSize: cons!CEF_RT_MAX_LIMIT_BATCHSIZE
          )
)

Is there a way to apply the filters in that order in a queryRecordType?
Somehow, sometimes the date range filter is applied first and then the other filters, since we want it to be applied on the MAX Measure result.

or any way to translate the SQL query we use to Appian.

  Discussion posts and replies are publicly visible

Parents Reply Children
  • Hi Stefan, our main goal is to select the last "Updated" history of each "CASE", because it can have multiple updates, and apply the filter of a date range to that selection.

  • This is another example of the issue:

    Have this Cade ID and we want the last date with "LogText"= "Updated", that should be 10/30/2024:

    IDCase Status
    24491 Active

    IDHistory  IDCase LogText CreatedDT
    499353 24491 Created 10/14/24 6:24
    499356 24491 Updated 10/14/24 6:25
    499357 24491 Updated 10/14/24 6:25
    499418 24491 Assigned 10/14/24 8:37
    499419 24491 Locked 10/14/24 8:37
    499420 24491 Assigned 10/14/24 8:41
    499421 24491 Updated 10/14/24 8:42
    499458 24491 Updated 10/14/24 12:54
    499459 24491 Locked 10/14/24 12:55
    499460 24491 Assigned 10/14/24 12:56
    499461 24491 Locked 10/14/24 12:56
    499462 24491 Updated 10/14/24 12:56
    1267064 24491 Updated 10/30/24 6:53

    So for example if the Filter for the dates are From 10-14-2024 To 10-14-2024 or From 10-01-2024 To 10-29-2024, it'll display the IDHistory "499462" (the orange one).

    But if the Filter for the dates are From 10-30-2024 To 10-30-2024 or From 10-01-2024 To 10-31-2024, it'll display the IDHistory "1267064" (the green one). 


    That's why I was looking for a way, that the Filter of the dates be apply on the result of the first filters, not at the same time as the others.