Skip to content
Advertisement

SQL, group multiple rows into 2 rows and make one row

I have such a table

Dates are subject to change, I want to group them in two rows. Dates range may be a maximum of 15 hours.

My expected output is

Advertisement

Answer

Assuming every start date always has a matching end date, we can try:

Here we are using pivoting logic along with the modulus of ROW_NUMBER to figure out which date records comes “first” and corresponds to the start date, and why comes “last” and corresponds to the end date.

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