skip to Main Content

I am newbie to sql, and this is my first time posting question here.

I created a table with 4 column. Item (which is Item no.) Description (the menu name) unit_price (price) and Qty (which is the order that has been made for that day).
I want the qty of every item so i write this query:

select distinct(Item),description,unit_price,qty
from cdsitem
order by item 

but the problem is it doesn’t add all the qty in the specific item, it looks like this.

p1

I want my output to become like this. the sum of quantity in every item no. and description

p2

I hope someone can help me. Thanks

2

Answers


  1. You want an aggregation query:

    SELECT item, description, unit_price, SUM(qty) AS qty
    FROM cdsitem
    GROUP BY 1, 2, 3;
    

    Or use the more verbose version:

    SELECT item, description, unit_price, SUM(qty) AS qty
    FROM cdsitem
    GROUP BY item, description, unit_price;
    
    Login or Signup to reply.
  2. select item,description,unit_price,sum(qty) as total_qty from cdsitem group by item;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search