So, the query I'm sharing here is not working the way I expect it to, and I'm just not sure why. I have a table of 6 different user roles, and a table of 4 different product versions, and a table of quizzes taken for the products. I have joined the three tables in my select, so that I can see the total number of quizzes taken by each role by each product. So, I want to see 24 rows (6x4), and expect a total for each of those rows of how many quizzes were taken by "that" role, for "that" product version. However, most of the values are actually zero, and only one role has taken any of the quizzes, and all the quizzes are for one product version today. Data will be getting larger here shortly. So, for 24 rows, I "should" only have one value that is not zero. I get 6 rows. The product version shows the same total for ALL of my user roles...and I cannot figure out why! Here's the query:
SELECT
[Role].[Description] AS Role,
[Asset].[ProductVersion],
Grade = SUM(CASE WHEN [UserTrainingLogEntry].[EventCode] = 'QUIZATTEMPT' THEN 1 ELSE 0 END)
FROM [UserTrainingLogEntry]
JOIN [Asset] ON [UserTrainingLogEntry].[PublishedAssetId] = [Asset].[PublishedAssetId]
CROSS JOIN [Role]
GROUP BY [Role].[Description], [Asset].[ProductVersion]
ORDER BY [Role].[Description], [Asset].[ProductVersion]
Here's the result:
Role | ProductVersion | Grade |
Administrators | 1.1 | 0 |
Administrators | 2 | 5 |
Administrators | 3 | 0 |
Administrators | 3.2 | 0 |
Cust01 Role | 1.1 | 0 |
Cust01 Role | 2 | 5 |
Cust01 Role | 3 | 0 |
Cust01 Role | 3.2 | 0 |
Everyone | 1.1 | 0 |
Everyone | 2 | 5 |
Everyone | 3 | 0 |
Everyone | 3.2 | 0 |
External Users | 1.1 | 0 |
External Users | 2 | 5 |
External Users | 3 | 0 |
External Users | 3.2 | 0 |
Mobile Device User | 1.1 | 0 |
Mobile Device User | 2 | 5 |
Mobile Device User | 3 | 0 |
Mobile Device User | 3.2 | 0 |
TrackAdmin | 1.1 | 0 |
TrackAdmin | 2 | 5 |
TrackAdmin | 3 | 0 |
TrackAdmin | 3.2 | 0 |
Where did I mess this up? Why do I get the '5' for grade in all my user roles, when it should only be for ONE of the roles.
ps. in case you are wondering why I didn't count instead of the sum, I actually do more stuff with the data here, but I simplified it for my testing and this post. :)
Thanks in advance!
mpleaf