?
Solved

SQL Server Count() and Distinct in Same Query

Posted on 2015-01-21
3
Medium Priority
?
74 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

777 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