mysql - Sum the values of a column based on a group of values from another column -


i have table each transaction associated given price. this:

transaction  price        1        10        2        20        3        30        5        50        6        10       10        10       23        10       24        10       25        10       26        10       27        10   

i'm trying find way sum price based on specific transaction or group of transactions. result of query this:

transaction  price    1-3        60    5-6        60     10        10  23-27        50 

this way can tell transactions 1 3 result 60, , on. can point me in right direction make using mysql?

since mysql doesn't have support windowing functions, have create our own group ranking table, , query operate on results.

select if(count(transaction) = 1, transaction, concat(min(transaction), '-', max(transaction))) transactions, sum(price) price    (     select if(`transaction` = @prev + 1,           if(@prev := `transaction`, @rank, @rank),            if(@prev := `transaction`, @rank := @rank + 1, @rank := @rank + 1)        ) gr,        `transaction`,        price     table1, (select @rank := 1, @prev := 0) q     order `transaction` asc   ) q        group gr 

demo here


Comments