Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Count by Value in MS SQL 2000 Query

Posted on 2006-06-25
6
Medium Priority
?
808 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 800 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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…

660 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