views:

276

answers:

9

Say you have table of some items with these two columns:

  • ItemName
  • Price

....where ItemName is unique.

How do you show sum of all prices for every ItemName (I'm using Oracle)? If I just try this:

SELECT ItemName, 
       SUM(Price)
  FROM Items

I get this error:

ORA-00937: not a single-group group function

...which goes away only if I use GROUP BY. But then I can SUM only by groups, not all of them.

+2  A: 

If ItemName is unique, won't the Price for each ItemName be whatever is associated with that ItemName?

If you're looking for the sum of all Item prices, you would have to leave out the ItemName from your SQL:

SELECT SUM(Price) FROM Items
Kaleb Brasee
A: 

Have you tried

 SELECT SUM(Price) FROM Items
James Keesey
A: 

If ItemName is unique, then the aggregated result will contain the same number of rows as the table. The SUM function will group by a column where duplicates exist to give you a SUM for all columns with that value:

SELECT ItemName, SUM(Price)
FROM [Table]
GROUP BY ItemName

Table Values:

 ItemName   Price
    ========   =====
    Foo        $1.00
    Bar        $5.00
    Bar        $5.00

Aggregate Result:

ItemName   Price
========   =====
Foo        $1.00
Bar        $10.00
Dave Swersky
+2  A: 

If you want the sum of all of them, why are you including ItemName in your query? You have two choices, include the ItemName and get the sum of all prices for that ItemName using "GROUP BY ItemName", or you can get the sum of all items, in which case you drop the "ItemName," from the select.

Paul Tomblin
A: 

You can't both group and not group in the same query. You can use a subquery to get the price:

select ItemName, (select sum(Price) from Items) as AllPrices
from Items

As the subquery doesn't use any data from the records in the main query, the database should be able to optimise it to only execute the subquery once.

Guffa
this will work thanks.
Daniel
Works, but inefficient: it queries the `Items` table twice.
Jeffrey Kemp
@Jeffrey: Yes, but querying the table twice is the only way to get that information in the result.
Guffa
A: 

It wants to you do:

select ItemName, sum(price)
from table
group by ItemName;

If you want the sum of ALL the items, what is the meaning of having the ItemName reported?

table:

item   price
spoon  2
spoon  4
fork   3
plate  6

For the above table, the sum is 15. What is the item name of the sum?

David Oneill
The title says: "Show sum of all for every record". I think he wants to show `15` `4` times.
Quassnoi
Good point. I can't imagine why he would want that, but you are correct.
David Oneill
+3  A: 
SELECT  ItemName, SUM(Price) OVER()
FROM    Items

This query will return you the list of ItemName's along with the total sum of prices against each ItemName:

SELECT  ItemName, SUM(Price) OVER()
FROM    (
        SELECT  'Apple' AS ItemName, 100 AS price
        FROM    dual
        UNION ALL
        SELECT  'Banana', 200 AS price
        FROM    dual
        UNION ALL
        SELECT  'Cocoa', 300 AS price
        FROM    dual
        ) q


ItemName  Price
--------  -----
Apple     600
Banana    600
Cocoa     600
Quassnoi
A: 

Since ItemName is unique, you need to use the following...

SELECT SUM(Price) FROM Items
pdavis
A: 

Perhaps what you're after is a list of items, their prices, as well as a Total of the prices?

SELECT ItemName, SUM(Price) Price
FROM Items
GROUP BY ROLLUP(ItemName);

ITEMNAME  PRICE
========  =====
Apples     1.00
Bananas    2.00
Cherries   3.00
           6.00
Jeffrey Kemp