This seems weird: [SQL FIDDLE]
The two users are definitely different i.e 1<>2. So why is the distinct count saying 1 ?
This seems weird: [SQL FIDDLE]
The two users are definitely different i.e 1<>2. So why is the distinct count saying 1 ?
user
is a reserved keyword. If you run:
select DateKey, User from the_table
you'll see that user is not 1
or 2
, but something like user_b1234
. Like Lamak suggested, use [user]
to refer to the column and not the system variable.
Here's an answer with some explanation...
User is not only a reserved word. It's more like a system defined read-only global variable.
It evaluates to the name of the current database user. So every row will get the same value and count(distinct user) is 1.
User produces the same value as the system defined USER_NAME() function.
So if you want User to be treated as a column name, write [User] as others have suggested, and as you did in your create table statement.
I think you want this:
select
COUNT(DateKey)
,count(distinct User) Cnt
from the_table
where Datekey = 20120103
group by
DateKey