sql - Show the total of a column for each group in its own row -
my select statement returns:
columna columnb columnc date "this" 5 3 "this" 5 3 "this" 5 3 "that" 10 5 "that" 10 5 "that" 10 5
i need select return this:
columna columnb columnc date "this" 5 3 "this" 5 3 "this" 5 3 total 15 9 "that" 10 5 "that" 10 5 "that" 10 5 total 30 15
i need group column , show total each group in own row. date populated in code.
all can think of selecting column possibilities, looping through columna names, selecting column name, unioning each of these each other. there better way this?
i think can grouping sets
. however, don't have unique identifiers each row. so, think logic:
with t ( select t.*, row_number() on (order (select null)) seqnum (<your query here>) t ) select columna, sum(columnb), sum(columnc) t group grouping sets((seqnum, columna), (columna));
Comments
Post a Comment