sql - Cursor? Loop? Aggregate up rows data along with row results -
sql - Cursor? Loop? Aggregate up rows data along with row results -
i have next table & data
table name = mytable
description | partition | total ------------|---------------|-------------- cash | reconciled | 25 cash | adjustm | 50 cash | balanc | 120 loans | adjustm | 44 loans | balanc | 32 cards | adjustm | 81 cards | balanc | 67 mtg | adjustm | 14 mtg | balanc | 92
the requirement simple plenty - it's straight select table, each unique description, need sum totals of partitions, such user see
description | partition | total ------------|---------------|-------------- cash | total | 195 < cash | reconciled | 25 cash | adjustm | 50 cash | balanc | 120 loans | total | 76 < loans | adjustm | 44 loans | balanc | 32 cards | total | 148 < cards | adjustm | 81 cards | balanc | 67 mtg | total | 106 < mtg | adjustm | 14 mtg | balanc | 92
it's stored proc i'm writing - don't have alternative of pulling mt perform need perform in body of stored proc. looking @ while loop or cursor provide roll need, or there glaringly obvious , easy solution i'm not seeing? aside roll up, it's straight
select * mytable
db sybase.
thanks
you can using grouping sets extension of group by
clause:
select description, coalesce(parition, 'total') partition, sum(total) total mytable grouping grouping sets ((description, partition), (description));
or use:
select description, coalesce(parition, 'total') partition, sum(total) total mytable grouping rollup (description, partition);
without rollup, can using union all
:
select description, parition, total mytable union select description, 'total' partition, sum(total) total mytable grouping description;
sql loops cursor sybase
Comments
Post a Comment