Skip to content
Advertisement

Max per group in sequential order in SQL

I have the below input:

I need output like this:

Query developed so far:

Not getting the startdate & c3 columns as expected. Any idea on this? Thanks in advance.

Advertisement

Answer

This is a gap-and-islands problem. You can use the difference of row numbers:

I’m not sure what the logic is for hid and c3, but max() seems to work for your sample data.

EDIT (based on comment):

To get c3 from the last row:

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