Solved

Count statement with group...

Posted on 2014-12-17
5
106 Views
Last Modified: 2014-12-18
Hello, can someone let me know how I can write a statement that firsts groups by column A and then give a count of values in column B. For eg;
Col-A(Cust ID)         Col-B
100                          Closed
200                          Active  
200                          Active
100                          Closed
300                          Open
400                          Progress
500                          Active

Output I am looking for is:
Col-B   count
Closed  1
Active    2
Open     1
Progress 1

Thanks for the help.

A
0
Comment
Question by:aej1973
  • 2
  • 2
5 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40506257
> firsts groups by column A and then give a count of values in column B.
Not quite sure I'm following your logic here, as the sample data set doesn't translate to the data mockup.  
There are seven rows in your sample data set, and only five in the result set.
Select ColB, COUNT(ColB) as the_count
FROM YourTable
GROUP BY ColB

Open in new window

0
 

Author Comment

by:aej1973
ID: 40506280
I am sorry if I did not make myself clear. Let me explain;

The first grouping should be with reference to  Col-A. So the outcome would be:
Col-A Col-B
100    Closed
200    Active
300    Open
400    Progress
500   Active

The next grouping and count should be with reference to col-B, the output will be;
Col b      count
Closed     1
Active      2
Open       1
progress 1

How can these two steps be combined into one query? Thanks for taking a look at it.

A
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40506313
Okay, try this then:
IF OBJECT_ID('tempdb..#tmp') IS NOT NULL
   DROP TABLE #tmp
GO

CREATE TABLE #tmp (ColA int, ColB varchar(25)) 

INSERT INTO #tmp (ColA, ColB)
VALUES 
   (100, 'Closed'), (200, 'Active'),  (200, 'Active'),(100, 'Closed'),
   (300, 'Open'),(400, 'Progress'),(500, 'Active')

SELECT a.ColB, COUNT(a.ColB) as [Count]
FROM (
   Select ColA, ColB
   FROM #tmp
   GROUP BY ColA, ColB) a
GROUP BY a.ColB

Open in new window

0
 
LVL 24

Expert Comment

by:chaau
ID: 40508156
All you need is to count DISTINCT values, like this:
Select ColB, COUNT(DISTINCT ColA) as the_count
FROM YourTable
GROUP BY ColB

Open in new window

0
 

Author Closing Comment

by:aej1973
ID: 40508274
Works! Thank you.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Loading csv or delimited data files to MySQL database is a very common task frequently questioned about and almost every time LOAD DATA INFILE comes to the rescue. Here we will try to understand some of the very common scenarios for loading data …
This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

21 Experts available now in Live!

Get 1:1 Help Now