I want to get the sum of these records, the condition is keep status=Processing if it exists in the records and the Latest UploadedOn with its corresponding UploadedBy
[RecordDate] [FileCount] [Status] [UploadedBy] [UploadedOn] 11/30/2017 1 On-queue tester1 12/21/2020 01:24:40.000000000 PM 12/31/2017 3 Success tester1 12/23/2020 10:26:45.000000000 AM 12/31/2017 3 Processing tester2 12/25/2020 10:26:45.000000000 AM
Currently I only get the sum(RecordDate) with the Max(UploadedOn) using this query
SELECT REPORTING_DATE, sum(FILE_COUNT), max(UPLOADED_ON) from IRREGULAR_FILES_UPLOADS group by REPORTING_DATE
And my wanted output is with
[RecordDate] [FileCount] [Status] [UploadedBy] [UploadedOn] 11/30/2017 1 On-queue tester1 12/21/2020 01:24:40.000000000 PM 12/31/2017 6 Processing tester2 12/25/2020 10:26:45.000000000 AM
Advertisement
Answer
Use Windows functions
this way :
WITH data AS ( SELECT RecordDate, FileCount, STATUS, UploadedBy, UploadedOn, ROW_NUMBER() OVER ( PARTITION BY RecordDate ORDER BY RecordDate) AS RN, SUM(FileCount) OVER (PARTITION BY RecordDate) AS SUM_FILECOUNT, MAX(UploadedOn) OVER (PARTITION BY RecordDate) AS MAX_UPLOADEDON FROM YOUR_TABLE ) SELECT RecordDate,SUM_FILECOUNT AS FileCount,STATUS,UploadedBy, MAX_UPLOADEDON AS UploadedOn FROM data WHERE RN = 1