Skip to content
Advertisement

SQL how to get total sum from all product rows without losing each product row from the view

I need to retrieve the total sum of all rows from this view, if I group by orderid the sum function works but then I lose each product row from the view and I don’t want that in this case. Is there a way to print the total sum (duplicated) for each row?

View:

Table printed:

Advertisement

Answer

You can use the WITH ROLLUP modifier to GROUP BY to get a summary row. However, because you have an invalid GROUP BY clause (not all of the non-aggregated columns are in the GROUP BY clause), you will get indeterminate values for the other columns in the ROLLUP summary line. However, p2o.produktid will be NULL so you can use that to blank the other values. For example:

Note that because your GROUP BY doesn’t include all non-aggregated columns, the values you see in the VIEW for those columns will be indeterminate, and may be different each time you SELECT * FROM v_invoice.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement