user3624426
user3624426

Reputation: 11

How to count total of values in the rows rather than total of rows in sql?

I have a columns:

Product Orders   Month    
Item1        1         1
Item1        2         1
Item1        1         1
Item2        1         1

I want to get total of items sold every month that result would be this:

Product TotOrders Month
Item1           4            1
Item2           1            1

If i use count(Orders) as TotOrders it gives me a result of 3 for Item1. Thats the total of rows, but I want a total of values in the rows (4 for item1).

Upvotes: 0

Views: 51

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726919

To compute a total, you use SUM function on the column that you need, and a group by:

select Product, sum(orders) TotOrders, month
from mytable
group by Product,month

Upvotes: 2

seeker
seeker

Reputation: 11

select Product, TotOrders as sum(Orders), Month 
from table group by Product, Month

Upvotes: 0

Related Questions