Avatar of Angela4eva
Angela4eva asked on

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
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Anthony Perkins

8/22/2022 - Mon
SOLUTION
Brian Crowe

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Éric Moreau

select time, count(*)
from (
   select disctinct time, agentname from YourTable
) as A
group by time
ASKER CERTIFIED SOLUTION
Anthony Perkins

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Anthony Perkins

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck