Skip to content

Tag: count

SQL Sorted Count

I have the following table sorted by date: date id 9/1/20 1 9/1/20 2 9/3/20 1 9/4/20 3 9/4/20 2 9/6/20 1 I’d like to add a count column for each id so that the first count for each id is the earliest date and latest date would receive the highest count for each id: date id count 9/1/20 1

Count seen users per week

My input table looks like: id user_id at 1 1 2020-12-07 13:07:35 2 794 2020-12-07 13:12:17 3 794 2020-12-07 13:12:26 4 794 2020-12-07 13:12:27 5 263 2020-12-07 13:12:50 6 263 2020-12-07 13:12:50 7 263 2020-12-07 13:13:10 8 1029 2020-12-07 13:18:07 9 1029 2020-12-07 13:18:07 10 378 2020-12-07 13:19:10 11 378 2…

Count only non duplicate values

I need a query to count only values that aren’t duplicates, is there any way to do this without subqueries or temp tables? If I have a table like this: I can’t use SELECT COUNT(DISTINCT quantity) because it returns 4. (89 | 40 | 32 | 29) How can I return 2? (89 | 32) Answer Use a subquery:

Get total count of duplicates in column

I need a query to count the total number of duplicates in a table, is there any way to do this? If I have a table like this: I can’t use SELECT COUNT(quantity) because it returns 2. (40 | 29) How can I return 5? (40 | 40 | 29 | 29 | 29) Answer Using analytic functions: