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 2012 Forums
 Transact-SQL (2012)
 SELECT Distinct Table1, Table2, Count(*)

Author  Topic 

danielhieb
Starting Member

2 Posts

Posted - 2013-02-12 : 19:21:27
SELECT DISTINCT Type, Version,
COUNT(*)
FROM STOVE
GROUP BY TYPE, VERSION;

Does Count(*) count just the selected columns(i.e. type, version)? If so, does it always count just the selected columns?

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2013-02-12 : 20:34:10
It counts all the rows in the group TYPE, VERSION. In your example, you don't even need the DISTINCT. If you want to count the distinct things in the group TYPE,VERSION, you can do something like

SELECT TYPE,VERSION, COUNT(DISTINCT InterestingColumn)
FROM STOVE
GROUP BY TYPE, VERSION;

If you want to know just how many different TYPE,VERSION combos you have, you can just do

SELECT Type, Version

FROM STOVE
GROUP BY TYPE, VERSION;

and see how many records there are, or do

SELECT COUNT(*) *
FROM
(

SELECT Type, Version
FROM STOVE
GROUP BY TYPE, VERSION
) t1


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

danielhieb
Starting Member

2 Posts

Posted - 2013-02-12 : 20:39:27
Thanks!

Daniel Hieb
Go to Top of Page
   

- Advertisement -