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
 Transact-SQL (2000)
 help w/ simple query

Author  Topic 

theworm91
Starting Member

2 Posts

Posted - 2005-04-18 : 11:59:59
It's been awhile since I have created any queries. Could somebody please help me with a simple query. I have 3 columns in a table 2 are numeric and one is a datetime. I will call them col1 col2 and col3. Here is what I would like to do in one query. I want to do a group by on col3 and produce a count of the distinct numbers. For example if there are 10 row in this table and col3 has the number 10 in it 3 time I would like the query to have the output of 3 for the count.
I hope this makes sense, please let me know if it doesn’t
Thanks

X002548
Not Just a Number

15586 Posts

Posted - 2005-04-18 : 12:34:27
[code]
USE Northwind
Go

SET NOCOUNT ON
CREATE TABLE myTable99(Col1 datetime, Col2 int, Col3 int)
GO

INSERT INTO myTable99(Col1, Col2, Col3)
SELECT '1/1/2005' , 1, 1 UNION ALL
SELECT '1/21/2005', 2, 10 UNION ALL
SELECT '2/14/2005', 3, 10 UNION ALL
SELECT '3/17/2005', 4, 10 UNION ALL
SELECT '4/1/2005' , 5, 15 UNION ALL
SELECT '5/30/2005', 6, 15 UNION ALL
SELECT '7/4/2005' , 7, 19 UNION ALL
SELECT '9/5/2005' , 8, 20
GO

SELECT Col3, COUNT(*) AS Occurs
FROM myTable99
GROUP BY Col3
GO

SET NOCOUNT OFF
DROP TABLE myTable99
GO

[/code]


Brett

8-)
Go to Top of Page

theworm91
Starting Member

2 Posts

Posted - 2005-04-18 : 12:45:51
Thank you, I will work with that.
Go to Top of Page
   

- Advertisement -