Jump to content
We've recently updated our Privacy Statement, available here ×

Aggregate Sum from top 4 ??


jimmyWhitetrix

Recommended Posts

Hello,

In the attached image, you can see that I have a student with 6 subject results. I trying to create a calculated column that will produce the Sum() of this student's top 4 subjects.

I am using JasperSoft Reports Server. The only function that seems of some use is Rank(). Typically I would use something like: LARGE(range,n), but I appreciate this is not an option.

Any help appreciated!

 

Many thanks,

Link to comment
Share on other sites

  • Replies 2
  • Created
  • Last Reply

Top Posters In This Topic

From a SQL perspective here is how you can do it using ROW_NUMBER.  I added RANK and DENSE_RANK so that you can determine how you would like to handle duplicate values.  I added a test duplicate value as *TST*.

select STUDENT, SUM(SCORE) AS TOP_4_SCOREFROM (    SELECT Student         , class_cd         , SCORE         , ROW_NUMBER() OVER (PARTITION BY student order by score desc) as rn         , RANK() OVER (PARTITION BY student order by score desc) as rnk         , DENSE_RANK() OVER (PARTITION BY student order by score desc) as drnk    FROM (             SELECT 'Charis' as Student, 'LIT' AS class_cd, 74 AS score             UNION ALL             SELECT 'Charis' as Student, 'REL', 82             UNION ALL             SELECT 'Charis' as Student, 'MAS', 85             UNION ALL             SELECT 'Charis' as Student, 'CHE', 87             UNION ALL             SELECT 'Charis' as Student, '*TST*', 87             UNION ALL             SELECT 'Charis' as Student, 'PHY', 88             UNION ALL             SELECT 'Charis' as Student, 'MAM', 91         ) A    ) BWHERE RN <= 4GROUP BY STUDENT;[/code]

 

Link to comment
Share on other sites

  • 4 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...