4

I have a table like given bellow, in Oracle:

[Products]
Product_ID | Product_NME | Product_SUP | Quantity
=================================================
    1         Apple         USA           100
    2         Fish          Japan          50
    3         Wine          Italy          10
    4         Apple         China          30
    5         Fish          Germany        10

I need a query that will find the full Quantity for every Product_NME by DISTINCT.

The expected result should be:

  • apple 130
  • fish 60
  • wine 10

I've tried to modify it like the one shown here as:

SELECT
    distinct(Product_NME, Product_SUP), sum(Quantity)
FROM
    Products

But it's not my case. Also I've tried this one:

SELECT DISTINCT  Product_NME
FROM Products 
UNION
SELECT SUM(Quantity) FROM Products 

But is also not working.

Can anyone help me with this?

  • Thanks
Community
  • 1
  • 1
ekostadinov
  • 6,880
  • 3
  • 29
  • 47

3 Answers3

12

DISTINCT is not the clause you are looking for!

GROUP BY is.

The following query will return with all products and the total quantity for each one.

SELECT
  Product_NME
  , SUM(Quantity) AS TotalQuantity
FROM
  Products 
GROUP BY
  Product_NME
Pred
  • 8,789
  • 3
  • 26
  • 46
0
SELECT Product_NME, SUM(Quantity) FROM Products GROUP BY Product_NME 
Felix Pamittan
  • 31,544
  • 7
  • 41
  • 67
0

You don't need for distinct just group by product name :

SELECT  Product_NME , SUM(Quantity) AS TotalQTY 

FROM  Products 

GROUP BY Product_NME
G.Nader
  • 847
  • 7
  • 9