user1570048
user1570048

Reputation: 880

sql sum some column when all other columns match

I have the following SQL temp table which is a result of an SQL query

enter image description here

I am trying to SUM columns numitems and ignoreditems where all the other columns match

I tried something like the following query:

SELECT 
    catalogid, 
    sum(numitems), sum(ignoreditesm) 
FROM ##temporderstable 
GROUP BY 
    catalogid, supplierid, cname, cprice, cstock, ccode, minstock, pother4

I can't seem to get it to work

I get an error

numitems doesn't belong to the table

although I can see it when I run the command

select * from ##temporderstable

Upvotes: 1

Views: 1111

Answers (1)

Robert
Robert

Reputation: 25763

Try to add aliases:

SELECT 
    T.catalogid, 
    sum(T.numitems) as numitems, 
    sum(T.ignoreditesm)  as ignoreditesm
FROM ##temporderstable T
GROUP BY 
    T.catalogid, T.supplierid, T.cname, T.cprice, 
    T.cstock, T.ccode, T.minstock, T.pother4

Upvotes: 1

Related Questions