Reputation: 199
Ok, so - IN SQL Server - I need to sum the count of records per week, and only show the week (and number of records in that week) that has the highest sum..
Up until now I can show each record, but I am only interested in the highest one. Any help would be appreciated.
*EDIT, since there is some confusion: I need a sum of occurances, grouped by week (so in this case: Project Q Unit 300 201435 2 is the highest, since the sum in week 38 for the same Project and unit combination is 1. ) The output should be as below.. What I get now is ALL Weeks with their respective sums, I only need to show the week with the Max sum
*hope that made sense :)
My table looks like this:
Project Name Subunit name Date of release Origin Week
Project X SubUnit 1 28-Jul-14 Unit 100 201431
Project X SubUnit 2 28-Jul-14 Unit 100 201431
Project X SubUnit 3 28-Jul-14 Unit 100 201431
Project X SubUnit 4 18-Sep-14 Unit 100 201438
Project X SubUnit 5 18-Sep-14 Unit 100 201438
Project X SubUnit 6 18-Sep-14 Unit 300 201438
Project X SubUnit 7 05-Aug-14 Unit 300 201432
Project X SubUnit 8 05-Aug-14 Unit 300 201432
Project X SubUnit 9 18-Sep-14 Unit 600 201438
Project X SubUnit 10 18-Sep-14 Unit 600 201438
Project X SubUnit 11 18-Sep-14 Unit 600 201438
Project X SubUnit 12 18-Sep-14 Unit 600 201438
Project Q SubUnit 1 18-Sep-14 Unit 100 201438
Project Q SubUnit 2 18-Sep-14 Unit 100 201438
Project Q SubUnit 3 18-Sep-14 Unit 100 201438
Project Q SubUnit 4 18-Sep-14 Unit 100 201438
Project Q SubUnit 5 18-Sep-14 Unit 100 201438
Project Q SubUnit 6 18-Sep-14 Unit 300 201438
Project Q SubUnit 7 29-Aug-14 Unit 300 201435
Project Q SubUnit 8 29-Aug-14 Unit 300 201435
Project Q SubUnit 9 29-Aug-14 Unit 600 201435
Project Q SubUnit 10 18-Sep-14 Unit 600 201438
Project Q SubUnit 11 18-Sep-14 Unit 600 201438
Project Q SubUnit 12 18-Sep-14 Unit 600 201438
Desired output is this:
Project Q Unit 100 201438 5
Project Q Unit 300 201435 2
Project Q Unit 600 201438 3
Project X Unit 100 201431 3
Project X Unit 300 201432 2
Project X Unit 600 201438 4
Upvotes: 2
Views: 612
Reputation: 35343
Didn't know RDBMS so I assumed SQL SERVER and therefore that a CTE (Common Table Expression) would work.
We can avoid the cte by using subqueries if needed though... Also I eliminated spaces in column names as I didnt' know if I should use ` or [ or " to escape the field names....
http://sqlfiddle.com/#!3/82c01/14/0
With CTE AS (SELECT ProjectName, Origin, Week, count(projectname) as myCount
FROM FOO
GROUP BY ProjectName, Origin, week)
SELECT A.ProjectName, A.Origin, A.Week, A.MyCount
FROM CTE A
INNER JOIN (SELECT ProjectName, Origin, Max(myCount) MaxMyCount
FROM CTE
GROUP BY ProjectName, Origin) B
on A.Projectname = B.ProjectName
and A.Origin = B.Origin
and A.myCount = B.MaxMyCount
ORDER BY ProjectName, Origin, Week
Now as to what this does:
In essence I generated 3 sets of data.
RDBMS is SET based logic the more you think of things in terms of sets and subsets the easier (in my opinion) it is to build such queries.
Upvotes: 1