Skip to content
Advertisement

Grabbing certain columns from the rows that the MAX value occurred

So i am trying to insert only the rows that are correlated to the HOUR where the MAX value occurred . However I am grouping by Day, and later on will be grouping on week, month, year, etc. Is my approach correct? The reason I am using a subquery is because I need to grab the value that goes into the HR column but adding it to the GROUP BY will mess up my daily groupings (if that makes sense). I need certain values from those columns to insert with a Stored Procedure that I wrote. Is the query I have below on the right track? Thanks in Advance

Below is some sample data:

What I want:

Also: Would the process be the same for weekly MAX as well? (of course I am going to convert the date/timestamp to IW (week), but aside from that)

Advertisement

Answer

One simple method uses row_number() or rank(). Here is an example, assuming you want the maximum for each data_code:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement