
November 13th, 2012, 08:12 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 18 m 14 sec
Reputation Power: 0
|
|
|
Joining of two queries
Team,
I am trying to write a query to display the following:
Pickerid, Pickername, Premiumcount, Rubbishcount
Each Pickerid has multiple entries in the table. ie
Pickerid Pickername Grade
1 test1 PR
1 test1 AP
1 test1 CA
1 test1 AM
1 test1 AA
and so on....
I am trying to get a grouped count which uses this logic.
Each Pickerid I need to count the number of "Grade" records equal to PR and AP. This we shall call Premiumcount, I also want to group the other "Grades" which are equal to "CA", "AM" & "AA" which we shall call Rubbishcount
These records all reside on the same table.
This is the result for the above I'm after.
Pickerid PickerName PremiumCount RubbishCount
1 test1 2 3
Each of my queries work individually but when I join them / union them I get no result.
I appreciate your help.
SELECT Q1.Pickerid, Q1.Pickername, PremiumCount, RubbishCount
FROM (SELECT Pickerid, Pickername, Count(*) AS PremiumCount FROM mergeddatabase WHERE (((Currentdate) between [Enter First Date Required:] and [Enter Last Date Required:]) and (grade = "PR" or grade = "AP" )) GROUP BY grade, PICKERID, PICKERNAME) AS Q1, (SELECT Pickerid, Pickername, Count(*) AS RubbishCount FROM mergeddatabase WHERE (((Currentdate) between [Enter First Date Required:] and [Enter Last Date Required:]) and (grade = "CA" or grade = "AM" or grade = "AA" )) GROUP BY grade, PICKERID, PICKERNAME) AS Q2
WHERE Q1.Pickerid = Q2.Pickerid;
Sorry for the formatting.. looks fine on my editor.
Regards
Mark
Last edited by witsendestate : November 13th, 2012 at 08:29 PM.
|