Solved

SQL Server Count() and Distinct in Same Query

Posted on 2015-01-21
3
65 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 46

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 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

25 Experts available now in Live!

Get 1:1 Help Now