Solved

Count by Value in MS SQL 2000 Query

Posted on 2006-06-25
6
799 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

738 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