Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 count distinct values

Author  Topic 

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2013-01-08 : 09:12:30
i have table with :
date,personalid
i want to count for each day
1)how many rows there are
2)how many distinct personalid in a day

what the best way to do it?

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2013-01-08 : 09:26:57
select count(*), count(distinct personalid), date
from tbl
group by date


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-08 : 23:49:01
does date field store timepart too? if yes,

select count(*), count(distinct personalid), dateadd(dd,datediff(dd,0,[date]),0)
from tbl
group by dateadd(dd,datediff(dd,0,[date]),0)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2013-01-09 : 03:58:35
10x all
Go to Top of Page
   

- Advertisement -