patternsqlMinor
Counting unique (distinct) users per day
Viewed 0 times
uniquedistinctcountingperusersday
Problem
I am new to
My table only has two columns-
Why does the following query not work? Shouldn't it be possible to select distinct on two variables at once?
produces
ERROR: function
The output would look like this
Any suggestions about how to count distinct min Date and user_id and then group by date in psql would be appreciated.
psql and am interested in counting the number of unique first time users per day. My table only has two columns-
user_id (there can be multiple uses by one user per day) and start_time which is a timestamp that indicates the time of use. The count should only include users whose min(start_time) fell on that day. Why does the following query not work? Shouldn't it be possible to select distinct on two variables at once?
SELECT COUNT (DISTINCT min(start_time::date), user_id),
start_time::date as date
FROM mytable
GROUP BY dateproduces
ERROR: function
count(date, integer) does not existThe output would look like this
date count
1 2017-11-22 56
2 2017-11-23 73
3 2017-11-24 13
4 2017-11-25 91
5 2017-11-26 107
6 2017-11-27 33...Any suggestions about how to count distinct min Date and user_id and then group by date in psql would be appreciated.
Solution
Because your form is off, what you want is
Or, in your case,
SELECT count(DISTINCT x)
FROM generate_series(1,10) AS gs(x);Or, in your case,
SELECT start_time::date, count(DISTINCT user_id)
FROM mytable
GROUP BY start_time::date;Code Snippets
SELECT count(DISTINCT x)
FROM generate_series(1,10) AS gs(x);SELECT start_time::date, count(DISTINCT user_id)
FROM mytable
GROUP BY start_time::date;Context
StackExchange Database Administrators Q#201624, answer score: 8
Revisions (0)
No revisions yet.