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 2005 Forums
 Transact-SQL (2005)
 distinct count of 2 attributes

Author  Topic 

plaidoe
Starting Member

4 Posts

Posted - 2008-09-09 : 11:34:20
Does anyone how to do a distinct count of 2 attributes from the same table?

For example:
SELECT COUNT (UNIQUE ord_id),COUNT(UNIQUE cust_id)
FROM Orders

Thank you!

bjoerns
Posting Yak Master

154 Posts

Posted - 2008-09-09 : 11:37:01
UNIQUE => DISTINCT
Go to Top of Page

plaidoe
Starting Member

4 Posts

Posted - 2008-09-09 : 11:43:08
Sorry I'm not sure what UNIQUE => DISTINCT means.

I get a syntax error msg when I run the example in original post. Is there another way to write this query to make it run?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-09 : 11:45:37
Bjoerns told you how to make your query work...

SELECT COUNT (DISTINCT ord_id), COUNT(DISTINCT cust_id)
FROM Orders


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

plaidoe
Starting Member

4 Posts

Posted - 2008-09-09 : 11:50:42
I see. I tried it and it didn't work.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-09 : 12:03:11
And why do you think it doesn't work?
Do you get an error? No records? To many records? Wrong kind of records? A dent in your computer screen?
Printer fire?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

bjoerns
Posting Yak Master

154 Posts

Posted - 2008-09-09 : 12:04:00
[code]
create table #table(
i1 int, i2 int
)
insert into #table
select 1, 1 union all
select 1, 2 union all
select 3, 3 union all
select 4, 2
select count(distinct i1), count(distinct i2)
from #table[/code]gives both 3 to me...
Go to Top of Page

plaidoe
Starting Member

4 Posts

Posted - 2008-09-09 : 12:05:15
[Error Code: -201, SQL State: 42000] A syntax error has occurred.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-09 : 12:26:16
Are you REALLY sure you are using MICROSOFT SQL Server 2005?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -