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
 General SQL Server Forums
 New to SQL Server Programming
 Return unique count
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ugh3012
Yak Posting Veteran

62 Posts

Posted - 09/06/2013 :  16:07:16  Show Profile  Reply with Quote

I need a query to return two values. One will be the total units and the other will be total unique units. See exmaple data below. It does not have to be one query. This will be in SP, so I can keep it seperate if I have to.



ID | ID_UNIT
1 | 01
1 | 01
1 | 02
1 | 03
1 | 03
1 | 04
1 | 04


I need two results.

Total Units = 7 - easy to do by using count()
Total unique units = 4 - I cannot use group by as it would return multiple results for each unit, which is not what we want.

Edited by - ugh3012 on 09/06/2013 16:08:09

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 09/06/2013 :  16:13:56  Show Profile  Reply with Quote
DECLARE @Foo TABLE (ID INT, ID_UNIT INT)

INSERT @Foo VALUES
(1, 01), 
(1, 01), 
(1, 02), 
(1, 03), 
(1, 03),
(1, 04), 
(1, 04)


SELECT
	COUNT(*) AS TotalUnits
	,COUNT(DISTINCT ID_UNIT)
FROM
	@Foo
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3661 Posts

Posted - 09/06/2013 :  16:14:48  Show Profile  Reply with Quote
SELECT COUNT(ID_UNIT), COUNT(DISTINCT ID_UNIT) FROM YourTable
Go to Top of Page

ugh3012
Yak Posting Veteran

62 Posts

Posted - 09/06/2013 :  16:23:42  Show Profile  Reply with Quote
Wow. I did not know you can put distinct in count function. I will try it on Monday. Many thanks and have a great weekend!!!!
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.08 seconds. Powered By: Snitz Forums 2000