sql server - SQL query sum()/join/order by/ - what's wrong? -


i learning sql, using northwind example database , i'm stuck on simple problem: want list products name in 1 column , total quantities sold each product in second column. first, did this:

select p.productid     ,sum(od.quantity) totalquantity  products p   join [order details] od      on p.productid=od.productid group p.productid 

and that's ok - have product id's , total quantities, when try this:

select p.productid     ,p.productname     , sum(od.quantity) totalquantity products p   join [order details] od      on p.productid=od.productid group p.productid 

i error:

msg 8120, level 16, state 1, line 1 column 'products.productname' invalid in select list because  not contained in either aggregate function or group clause.` 

when use aggregate functions (like sum) have group by every other field in select

you have modify query follows

select p.productid, p.productname, sum(od.quantity) totalquantity products p  join [order details] od on p.productid=od.productid group p.productid, p.productname 

Comments

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -