Skip to content
Advertisement

Group rows of data from table based on a column

I want to achieve the below using SQL can anyone please help:

Should become:

Basically wanting to group the data for same consecutive RockSymbol, where in the result the TOP of first record of the consecutive block becomes the TOP of the grouped record and the BOTTOM of last record of the consecutive block becomes the BOTTOM of the grouped record.

The number of consecutive RockSymbol records can vary.

Advertisement

Answer

Updated answer – considering situation where we can have multiple consecutive rows with same RockSymbol.

SQl fiddle link http://sqlfiddle.com/#!6/dc3c3/3

Edit: Old answeronly for the purpose of understanding the evolution of solution

If you can mutate original table then probably update statement

followed by removal of extra rows

If you cannot update table please use a temp table or nested SELECT query like below

Sql fiddles link for both solution are here http://sqlfiddle.com/#!6/e1148/1/0 and http://sqlfiddle.com/#!6/e1148/7

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