Skip to content
Advertisement

SQL SUM OVER PARTITION BY 2 columns not working

Common question, I know. Just haven’t been able to find a solution to my question, so hit me with the removal and or downvotes if you must.

(Oracle 12c)

I have data that looks like this:

I want to get QTY to aggregate (sum) at the Date and item level with only one record for each unique item on each date, so it looks like this:

The query I’m trying to use to do this is:

Advertisement

Answer

A date data type in Oracle can have a time component. So you need to be careful. Unless you know hat you have no time component, trunc() is safer. Also, you can use the date keyword to handle date constants: SELECT

Notes:

  • You don’t need an analytic function. Aggregation should be sufficient.
  • Use proper, explicit, standard, readable JOIN syntax.
  • I assume the column in the ORDER BY is intended to be the first column in the result set.
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement