I am using AWS Athena to query count of distinct values for a column for last 7 days.
The query is invoked by a lambda function which is invoked on every Sunday of the month and pulls data from last Sunday to this Saturday.
So, for example, If today is 11th September 2022, Sunday, then the lambda will try to query the table from 4th Sep '22, Sunday till 10th Sep '22, Saturday and the query looks like this.
SELECT
col1,
col2,
COUNT(DISTINCT col3) AS distinctValues
FROM "dbName"."tbl"
WHERE year = '2022'
AND month = '09'
AND day IN ('04','05','06','07','08','09','10' )
GROUP BY
col1,
col2;
year, month and day are different columns and therefore we have IN clause for day column.
Now the issue is, if the query has to be run on 4th September 2022, then two months have to be considered. The query has to be run to get data from 28th Aug '22, Sunday to 3rd Sep '22, Saturday.
I cannot run this query to get the data as it will not contain correct count of distinct values.
SELECT
col1,
col2,
COUNT(DISTINCT col3) AS distinctValues
FROM "dbName"."tbl"
WHERE year = '2022'
AND month IN ('08','09')
AND day IN ('28','29','30','31','01','02','03' )
GROUP BY
col1,
col2;
And I can also not process the results from two separate queries for Aug and Sep months because distinct values will not be correct.
What can be done here to get distinct values for date ranges spread between two months and considering the table schema that year, month and day are stored in different columns?