I have a table which contains chronological events. They have a type, time and height.
The data can be summarized like so:
ID, Type, Time, Height
1, A, time, 0
2, XX, time, 0
3, B, time, 3
4, B, time, 6
5, C, time, 0
6, XX, time, 0
7, A, time, 0
8, C, time, 0
9, A, time, 0
10, B, time, 2
11, C, time, 0
etc ( the time column is sorted in ascending order)
i would like to find a SQL statement to list all types of A/B/C where B is the maximum of the height column between types A and C.
So the output would look like:
1, A, time, 0
4, B, time, 6
5, C, time, 0
7, A, time, 0
8, C, time, 0
9, A, time, 0
10, B, time, 2
11, C, time, 0
The A/B and C will always be in correct order (i.e. B will always be between A and C), But there may not be a B at all, or there may be multiple B’s between A and C.
The output may/may not list a B event with NULL data if there is no B between A and C.
There is guaranteed to be a C after every A type event.
All XX events shall be ignored in the output. Timestamps over the list will never be duplicated – no two events will contain the same time.
Im guessing to use the MAX function somewhere, and to select all B Rows between A and C depending on the time of A and C.
TIA
Not sure if I have this 100% right, but I find that it’s always best to break this stuff down into smaller queries into temp tables. Here’s a crack at it… (BTW – this is SQL Server T-SQL)
Depending on how you want to handle ties for max height, you may want to modify that second query’s
ORDER BYclause. Good luck.