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
SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

select time, count(*)
from (
   select disctinct time, agentname from YourTable
) as A
group by time
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
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
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo