I need some help finding the sum of all rows grouped by orderid. Each row is a different product purchased on an order. Some orders had more than one product purchased (hence why there are multiple rows per orderid). The total column is the total $ for each orderid.
SELECT OrderID, SUM(UnitPrice*Quantity*(1-Discount)) OVER (PARTITION BY OrderID) as Total_Invoice
FROM [Order Details]
ORDER BY OrderID
How do I get only one row for each invoice? So results looks like this:
| order_id | total_invoice |
| -------: | ---------------------: |
| 10248 | 440 |
| 10249 | 1863.39999389648 |
| 10250 | 1552.60003662109 |
| 10251 | 654.060005187988 |
| 10252 | 3597.89990234375 |
I would put Distinct right after the select. I would do this because you have sum over partition by
Your current code:
I would change this to:
If you did not want the partition by, you could do this: