kk_nou kk_nou - 1 month ago 6
SQL Question

Raw data before each group by

Which queries should I execute in order to show raw data before every group by row? Is there a way to do that?

User ReceiptNo Price
----- ---------- ------
A 876 100
A 877 50
B 960 60
B 961 40


Most database support standard group by modifiers such as rollup or grouping sets. If so, you can do:

select user, receiptno, sum(price) as price
from t
group by grouping sets ((user, receiptno), (user));