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:
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
Filters in Appian are always applied at the same time.
I do not fully understand what you are doing with this data. If you share your thoughts, we might be able to come up with some ideas.
ceciliaa9949 let me try and summarize what i understood
You have two tables:
IDCase
Status
LogText
CreatedDT
The requirement:
LogText = 'Updated'
Status = 'Close'
Current SQL solution:
Here’s how to translate the SQL query into Appian:
Record Type Setup: Ensure that HistoryCase is a record type with a relationship to the CASE record type.
HistoryCase
CASE
Appian Query for Aggregation: follows next message <CODE>
Explanation of the Solution:
MAX
batchSize
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 ) )