Solved

SQL: COUNT each of several values in one field

Posted on 2006-11-10
4
461 Views
Last Modified: 2012-05-05
I'm convinced there's an easy way to do this, but heck if I can figure it out.  My environment is Oracle.

I have a query that aggregates some of the data from a table so that the query result is a single row, like this:

TTL_COUNT | AVG_PRICE  |  AVG_AGE  |  TTL_PRICE  
       15        |       500       |       25       |       32672  

I have another field ('GRADE'), and I'd like to show the count of each value in that field (A-D).  So my result would look like:

TTL_COUNT | AVG_PRICE  |  AVG_AGE  |  TTL_PRICE  |  COUNT_A  | COUNT_B | COUNT_C | COUNT_D
       15        |       500       |       25       |       32672    |       6         |       8       |       0       |       1

The only way I've found make this work is with a subquery in my FROM clause for each value, which gets ridiculously long:

SELECT
   /*existing code above here*/
  COUNT(GRADE_A.key) AS COUNT_A,
  COUNT(GRADE_B.key) AS COUNT_B,
  COUNT(GRADE_C.key) AS COUNT_C,
  COUNT(GRADE_D.key) AS COUNT_D
FROM
  main_table,
  (SELECT key
     FROM main_table
     WHERE GRADE='A'
  ) GRADE_A,
  (SELECT key
     FROM main_table
     WHERE GRADE='B'
  ) GRADE_B,
  (SELECT key
     FROM main_table
     WHERE GRADE='C'
  ) GRADE_C,
  (SELECT key
     FROM main_table
     WHERE GRADE='D'
  ) GRADE_D
WHERE 1=1
  /*criteria for limiting records above here*/
  /*what follows are the joins that limit the subqueries to the same criteria the rest of the query uses*/
  AND main_table.key=GRADE_A.key(+)
  AND main_table.key=GRADE_B.key(+)
  AND main_table.key=GRADE_C.key(+)
  AND main_table.key=GRADE_D.key(+)


Any input is appreciated.
0
Comment
Question by:Rakafkaven
[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
  • 2
4 Comments
 
LVL 4

Author Comment

by:Rakafkaven
ID: 17918344
If the lines at the bottom are confusing any non-Oracle users, think of them as LEFT JOINs from the main_table to the four subtables.  I'm assuming that if there is a better solution, it won't necessarily be Oracle-specific, which is why I tried this on the main DB page.
0
 
LVL 6

Accepted Solution

by:
Ritesh_Garg earned 500 total points
ID: 17919516
There is a better way to do it:

SELECT
   /*existing code above here*/
  COUNT(DECODE(GRADE, 'A', 1, NULL)    COUNT_A,
  COUNT(DECODE(GRADE, 'B', 1, NULL)    COUNT_B,
  COUNT(DECODE(GRADE, 'C', 1, NULL)    COUNT_C,
  COUNT(DECODE(GRADE, 'D', 1, NULL)    COUNT_D,
FROM
  main_table
/

Count ignores null values therefore the decode will return 1 only when the GRADE = A and so on...

Hope this helps.
0
 
LVL 5

Expert Comment

by:MrNetic
ID: 17921314
RacafKaven,

You could do something like,

Select count(*),ColumnName from TABLENAME

Group by ColumnName

It will answer you questions.

Best Regads,

Paulo Condeça.
0
 
LVL 4

Author Comment

by:Rakafkaven
ID: 17931390
Ritesh, that's perfect!  Don't know why I never think of using conditionals in my SQL.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 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