How to query distinct from AWS log insights
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
CloudWatch Logs Insights can answer "distinct" questions, but not with the exact SQL syntax many people expect. The right solution depends on whether you want one representative log event per value or you only want the count of unique values.
Distinct Rows and Distinct Counts Are Different
In Logs Insights, there are two main patterns:
- Use
dedupwhen you want one event per unique field or field combination. - Use
stats count_distinct(field)when you want the number of unique values.
That difference matters because the output is different. dedup still returns log events, while count_distinct returns an aggregate number.
For example, if you want the most recent event for each user ID, you should use dedup. If you want to know how many unique users appeared in the last hour, you should use count_distinct.
Use dedup to Keep One Event Per Value
dedup removes duplicates based on the fields you name. The important detail is that it keeps the first matching event according to the current sort order, so sorting first is usually the correct move.
Related reading
- How to re-partition pyspark dataframe?
- How to read a csv file from an s3 bucket using Pandas in Python
- How to read a Parquet file into Pandas DataFrame?
- How to read a .xlsx file using the pandas Library in iPython?
- How to query DynamoDB by date range key, with no obvious hash key?
- How to query DynamoDB filtering by value in a list
- How to read binary files in Python using NumPy?
- How to read data from numpy files in TensorFlow?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.