Advertisement

05.30.2008 at 12:38PM PDT, ID: 23445796
[x]
Attachment Details

How to get totals of each group in SQL

Asked by mark_goodall in SQL Query Syntax

Tags: SQL, SQL, SQL

How do I get a secondary COUNT in an SQL query?
I have a table called ITEMS as shown below. I have an SQL query with a GROUP BY giving me a report, but I need more detail in the report.
What SQL query do I need to get the desired report?  I'm doing this in ASP(classic) if that matters.

Thanks!
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
Sample table data:
 
LIST   - CODE - STATUS
-------------------------
BANNER -  01  - A
BANNER -  02  - A
BANNER -  03  - A
BANNER -  04  - F
BANNER -  05  - R
BANNER -  06  - R
BANNER -  07  - R
BANNER -  08  - R
NEWS   -  01  - A
NEWS   -  02  - A
NEWS   -  03  - F
NEWS   -  04  - F
NEWS   -  05  - A
PEOPLE -  01  - A
PEOPLE -  02  - A
PEOPLE -  03  - A
PEOPLE -  04  - A
PEOPLE -  05  - A
PEOPLE -  06  - F
 
 
 
 
This SQL query:
 
SELECT LIST, COUNT(*) AS COUNTER FROM ITEMS WHERE STATUS<>'X' GROUP BY LIST;
 
Produces this report:
 
LIST - COUNT
----------------- 
BANNER - COUNT= 8
NEWS   - COUNT= 5
PEOPLE - COUNT= 6
 
 
What SQL query do I need to submit to get a report more like this:
 
BANNER - COUNTSTATUS-A = 3 - COUNTSTATUS-F = 1 - COUNTSTATUS-R = 4 
NEWS   - COUNTSTATUS-A = 2 - COUNTSTATUS-F = 2 - COUNTSTATUS-R = 0
PEOPLE - COUNTSTATUS-A = 5 - COUNTSTATUS-F = 1 - COUNTSTATUS-R = 0
[+][-]05.30.2008 at 03:20PM PDT, ID: 21681807

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.30.2008 at 07:35PM PDT, ID: 21682482

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.30.2008 at 08:48PM PDT, ID: 21682660

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: SQL Query Syntax
Tags: SQL, SQL, SQL
Sign Up Now!
Solution Provided By: mark_wills
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.31.2008 at 04:57AM PDT, ID: 21683469

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628