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 2000 Forums
 SQL Server Development (2000)
 How to group and count

Author  Topic 

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2006-07-23 : 07:52:04
In a Order table, there are records like bellow:
OrderID: 111,222,333 222, 222, 333
How to code to count(OrderID) = 3 not = 6 ?

I used code bellow but did not work:

select count(OrderID) from Order group by OrderID

Kristen
Test

22859 Posts

Posted - 2006-07-23 : 08:11:55
This maybe?

select count(DISTINCT OrderID) from Order

Kristen
Go to Top of Page

2400hrs
Starting Member

4 Posts

Posted - 2006-07-23 : 08:15:52
[code]
SELECT COUNT(DISTINCT OrderID) FROM [Order]
[/code]

Your SQL is counting the OrderIDs per grouping, so you will get something like:

1
2
3

If written

[code]
SELECT OrderID, COUNT(OrderID) OrderCount
FROM [Order]
GROUP BY OrderID
[/code]

You would see what it's doing:

[code]
Order ID OrderCount
111 1
222 3
333 2
[/code]
Go to Top of Page

Sun Foster
Aged Yak Warrior

515 Posts

Posted - 2006-07-23 : 09:22:16
It works, thank both of you!!!
Go to Top of Page
   

- Advertisement -