Solved

Count by Value in MS SQL 2000 Query

Posted on 2006-06-25
6
804 Views
Last Modified: 2012-05-05
I have a table with 4 columns : JobID, TechnicianID, Grade, Points
Each time a technician completes a job it will logged on to this table with proper grade (A,B,C,F) and numeric points.
I am trying to generate a report that sums the points and counts grade by technician.
So, the report will look like this:

TechID    TotalPoints    A count   B count    C count
123         900                5            2             7
234         850                4            1             6
....          ....                 .....................................

Is this possible with one query?

Thanks for your time.
0
Comment
Question by:JOSHUABT
[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
  • 2
6 Comments
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 16981220
You could do this:
SELECT TechnicianID, Grade, SUM(Points) AS 'Points'
FROM JobLog
GROUP BY TechnicianID, Grade

But that would not show each grades on one row.
0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 16981268
I played a little more with it. How about this:

SELECT TechnicianID, , SUM(Points) AS 'TotalPoints', COUNT(*)-COUNT(NULLIF(Grade, 'A')) AS 'A count', COUNT(*)-COUNT(NULLIF(Grade, 'B')) AS 'B count', COUNT(*)-COUNT(NULLIF(Grade, 'C')) AS 'C count'
FROM JobLog
GROUP BY TechnicianID
0
 
LVL 26

Assisted Solution

by:DireOrbAnt
DireOrbAnt earned 200 total points
ID: 16981276
Sorry, double ,, here it is:
SELECT TechnicianID, SUM(Points) AS 'TotalPoints', COUNT(*)-COUNT(NULLIF(Grade, 'A')) AS 'A count', COUNT(*)-COUNT(NULLIF(Grade, 'B')) AS 'B count', COUNT(*)-COUNT(NULLIF(Grade, 'C')) AS 'C count'
FROM JobLog
GROUP BY TechnicianID
0
Three Considerations for Containers

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read our article on Experts Exchange.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 16981291
select techid, sum(points)
, sum(case when Grade='A' then 1 else 0 end) as A_Count
, sum(case when Grade='B' then 1 else 0 end) as B_Count
, sum(case when Grade='C' then 1 else 0 end) as C_Count
, sum(case when Grade='F' then 1 else 0 end) as F_Count
from yourtable
group by techid
0
 
LVL 5

Expert Comment

by:rmacfadyen
ID: 16981308
This looks like a "cross-tab" query... though of aggregates. SQL 2005 has some new support for cross-tab queries... but I haven't dug into it much at all (ie. if you're using SQL2005 look in books on line for cross tab).

Typically, where your number of columns are constrained (fixed in number) and reasonably small you can do the above with correlated sub-queries:

select
    TechId,
    sum(Points) as TotalPoints,
    (select count(*)
     from  Grades as g2
     where g2.TechId = Grades.TechId
         and g2.Grade = 'A') as A_Count,
    (select count(*)
     from  Grades as g2
     where g2.TechId = Grades.TechId
         and g2.Grade = 'B') as B_Count,
    (select count(*)
     from  Grades as g2
     where g2.TechId = Grades.TechId
         and g2.Grade = 'C') as C_Count,
    (select count(*)
     from  Grades as g2
     where g2.TechId = Grades.TechId
         and g2.Grade = 'F') as F_Count
from Grades
group by TechId

Note... I followed your columns of A, B, C and F (no D or E).

With the above performance should be ok with an index by techid and grade... but the execution plan should be checked carefully.

Also... the above style can be somewhat fragile and a pain to maintain. Adding a new column means monkeying about with the SP. If the same sort of data is queried in several SP's then these will also need to be updated... and it may be easy to "miss" one.

It may also be worth adding an extra column "Unknown_Grade_Count" for:
    (select count(*)
     from  Grades as g2
     where g2.TechId = Grades.TechId
         and g2.Grade not in ('A', 'B', 'C', 'F') as Unknown_Grade_Count

But... that may well involve a table scan... so caution is required.

Also... you could "push back" to the consumers of the query to do the "cross tabbing" themselves (this is relatively straight-forward to do in VB/C#/C++/etc). And many 3rd party reporting tools will do "cross tab" reports that will do this kind of thing for you (in Excel this sort of thing is called a pivot table).

Regards,

Rob
0
 
LVL 5

Expert Comment

by:rmacfadyen
ID: 16981311
Dang... I like DireOrbAnt and AngelIII solutions much better than mine :(

Must have sub-queries on the brain :)

Disregard!

Rob
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

623 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