sql query count distinct

classes      time      agentname
2      8/26/13 6:00      a1
2      8/26/13 6:00      a2
1      8/26/13 6:00      a3
1      8/26/13 6:00      a1
1      8/26/13 6:00      a5
1      8/26/13 6:15      a1
1      8/26/13 6:15      a2
1      8/26/13 6:15      a3
2      8/26/13 6:15      a4
1      8/26/13 6:15      a5
2      8/26/13 6:30      a2
2      8/26/13 6:30      a3
2      8/26/13 6:30      a5



I want to count the distinct agents

so
8/26/13 6:00---4
8/26/13 6:15--5

As you can see there were 5 records for 8/26/13 6:00 but count shows only 4 as they were only 4 distinct agents.
I am using sql server 2000
Angela4evaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
SELECT time, COUNT(*)
FROM (
   SELECT DISTINCT time, agentname
   FROM myTable ) AS A
GROUP BY time
0
Éric MoreauSenior .Net ConsultantCommented:
select time, count(*)
from (
   select disctinct time, agentname from YourTable
) as A
group by time
0
Anthony PerkinsCommented:
Like this perhaps:
SELECT  [time],
        COUNT(DISTINCT agentname)
FROM    YourTableName
GROUP BY [time]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Anthony PerkinsCommented:
This is how I tested it:
DECLARE	@Table TABLE (classes tinyint, [time] smalldatetime, agentname char(2))
INSERT	@Table (classes, [time], agentname)
VALUES	(2,  '8/26/13 6:00',  'a1'),
		(2,  '8/26/13 6:00',  'a2'),
		(1,  '8/26/13 6:00',  'a3'),
		(1,  '8/26/13 6:00',  'a1'),
		(1,  '8/26/13 6:00',  'a5'),
		(1,  '8/26/13 6:15',  'a1'),
		(1,  '8/26/13 6:15',  'a2'),
		(1,  '8/26/13 6:15',  'a3'),
		(2,  '8/26/13 6:15',  'a4'),
		(1,  '8/26/13 6:15',  'a5'),
		(2,  '8/26/13 6:30',  'a2'),
		(2,  '8/26/13 6:30',  'a3'),
		(2,  '8/26/13 6:30',  'a5')

SELECT  [time],
        COUNT(DISTINCT agentname)
FROM    @table
GROUP BY [time]

Open in new window

Here is the output:
2013-08-26 06:00:00      4
2013-08-26 06:15:00      5
2013-08-26 06:30:00      3
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.