kk_nou kk_nou - 3 months ago 24
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
**150**
B 960 60
B 961 40
**100**

Answer

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));