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
  • 0
    Certified Associate Developer

       let me try and summarize what i understood

    1. You have two tables:

      • Main Table (CASE): Contains IDCase and Status for cases.
      • Secondary Table (HistoryCase): Stores the historical movements (LogText, CreatedDT) for each case.

    2. The requirement:

      • Find the last "Update" (LogText = 'Updated') for each case.
      • Exclude cases with Status = 'Close'.
      • Apply a date range filter on the CreatedDT field (e.g., from 2024-11-01 to 2024-11-30).
      • Order the results by IDCase.

    3. Current SQL solution:

      • A subquery identifies the latest update for each case.
      • A date range filter is applied on the CreatedDT field in the final result.


    Here’s how to translate the SQL query into Appian:

    1. Record Type Setup: Ensure that HistoryCase is a record type with a relationship to the CASE record type.

    2. Appian Query for Aggregation: follows next message <CODE>

     

    Explanation of the Solution:

    • Aggregation Fields: Group by IDCase to aggregate data for each case.
    • Measure: Use the MAX function to fetch the latest CreatedDT for each case.
    • Filters:
      • Only include rows where LogText starts with "Updated."
      • Exclude cases with Status = 'Close' using a filter on the CASE record type.
      • Filter the results to a specific date range using CreatedDT.

    • Paging: Adjust the batchSize as needed.

     

  • 0
    Certified Associate Developer
    in reply to naveenkumar11800

    a!queryRecordType(
      recordType: 'recordType!HistoryCase', /* Replace with your record type */
      fields: {
        a!aggregationFields(
          groupings: {
            a!grouping(
              field: 'recordType!HistoryCase.IDCase',
              alias: "IDCaseGroup"
            )
          },
          measures: {
            a!measure(
              function: "MAX",
              field: 'recordType!HistoryCase.CreatedDT',
              alias: "LatestUpdate"
            )
          }
        )
      },
      filters: a!queryLogicalExpression(
        operator: "AND",
        filters: {
          a!queryFilter(
            field: 'recordType!HistoryCase.LogText',
            operator: "starts with",
            value: "Updated"
          ),
          a!queryFilter(
            field: 'recordType!CASE.Status',
            operator: "<>",
            value: "Close"
          ),
          a!queryFilter(
            field: 'recordType!HistoryCase.CreatedDT',
            operator: "between",
            value: {
              todatetime(ri!fromDate),
              todatetime(ri!toDate) + 1 /* Include the end date */
            }
          )
        },
        ignoreFiltersWithEmptyValues: true
      ),
      pagingInfo: a!pagingInfo(
        startIndex: 1,
        batchSize: cons!DEFAULT_BATCH_SIZE
      )
    )
    

Reply
  • 0
    Certified Associate Developer
    in reply to naveenkumar11800

    a!queryRecordType(
      recordType: 'recordType!HistoryCase', /* Replace with your record type */
      fields: {
        a!aggregationFields(
          groupings: {
            a!grouping(
              field: 'recordType!HistoryCase.IDCase',
              alias: "IDCaseGroup"
            )
          },
          measures: {
            a!measure(
              function: "MAX",
              field: 'recordType!HistoryCase.CreatedDT',
              alias: "LatestUpdate"
            )
          }
        )
      },
      filters: a!queryLogicalExpression(
        operator: "AND",
        filters: {
          a!queryFilter(
            field: 'recordType!HistoryCase.LogText',
            operator: "starts with",
            value: "Updated"
          ),
          a!queryFilter(
            field: 'recordType!CASE.Status',
            operator: "<>",
            value: "Close"
          ),
          a!queryFilter(
            field: 'recordType!HistoryCase.CreatedDT',
            operator: "between",
            value: {
              todatetime(ri!fromDate),
              todatetime(ri!toDate) + 1 /* Include the end date */
            }
          )
        },
        ignoreFiltersWithEmptyValues: true
      ),
      pagingInfo: a!pagingInfo(
        startIndex: 1,
        batchSize: cons!DEFAULT_BATCH_SIZE
      )
    )
    

Children
  • Hello, thank you for your response.

    The problem persist cause the Filters in Appian applied at the same time, For example:

    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.