SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 SELECT Distinct Table1, Table2, Count(*)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

danielhieb
Starting Member

USA
2 Posts

Posted - 02/12/2013 :  19:21:27  Show Profile  Reply with Quote
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?

Edited by - danielhieb on 02/12/2013 19:22:09

jimf
Flowing Fount of Yak Knowledge

USA
2869 Posts

Posted - 02/12/2013 :  20:34:10  Show Profile  Reply with Quote
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

USA
2 Posts

Posted - 02/12/2013 :  20:39:27  Show Profile  Reply with Quote
Thanks!

Daniel Hieb
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000