Solved

SQL Server Count() and Distinct in Same Query

Posted on 2015-01-21
3
69 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 49

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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

726 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