Solved

Count by Value in MS SQL 2000 Query

Posted on 2006-06-25
6
796 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 extract information from SQL Server on Database, Connection and Server properties

828 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