Solved

Count by Value in MS SQL 2000 Query

Posted on 2006-06-25
6
789 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
  • 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 142

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

757 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

19 Experts available now in Live!

Get 1:1 Help Now