Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Sum not working with Cross Join as I need

$
0
0

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:

RoleProductVersionGrade
Administrators1.10
Administrators25
Administrators30
Administrators3.20
Cust01 Role1.10
Cust01 Role25
Cust01 Role30
Cust01 Role3.20
Everyone1.10
Everyone25
Everyone30
Everyone3.20
External Users1.10
External Users25
External Users30
External Users3.20
Mobile Device User1.10
Mobile Device User25
Mobile Device User30
Mobile Device User3.20
TrackAdmin1.10
TrackAdmin25
TrackAdmin30
TrackAdmin3.20

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

 

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>