Solved

SQL Server Count() and Distinct in Same Query

Posted on 2015-01-21
3
64 Views
Last Modified: 2015-01-21
I have an employee table with 4 columns (id, type, class, and name). I want to count the number of employees in the table for each unique combination of type and class. So if the table looks like:

ID           Type          Class         Name
123           I                  A            Smith
124           I                  A            Jones
129           I                  C            Franklin
125          I I                 A            Jenkins
126          I I                 B            Smith

I want my result set to look like:

Type       Class       Count
   I               A               2
   I               C               1
  I I              A               1
   I               B               1

I've tried the following but SQL Server doesn't like it

select count(*)
from (
select distinct type, class from employee)

Any ideas?

Thanks in advance for your help
0
Comment
Question by:cbridgman
3 Comments
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40562234
SELECT Type, Class, Count(1) qty
FROM employee
GROUP BY Type, Class

Open in new window

0
 
LVL 12

Expert Comment

by:FarWest
ID: 40562235
did you try this?

select count(*),type,class from employee
group by type,class
0
 

Author Closing Comment

by:cbridgman
ID: 40562610
Thanks for the feedback. I searched all over the place for the answer to this and couldn't find anything simple like this. Works like a charm.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now