I am newbie in sql server
i'm using sql server 2005 and the northwind database
i wanted to join order and order details tables and calculate the total purchase made by each customer by using group by clause.
When i used
select orders.orderid,orders.customerid,orders.orderdate,orders.shipcity,orders.shipcountry,
orderdetails.unitprice,orderdetails.quantity,(orderdetails.unitprice*orderdetails.quantity )as total from orders
inner join orderdetails on orders.orderid=orderdetails.orderid group by orders.customerid
i received the error message Column 'orders.OrderID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Can anyone explain the reason for the error message
As other way around i tried to create a view and then use the Group by clause as below
create view Purchase_by_each_customer
as
select orders.orderid,orders.customerid,orders.orderdate,orders.shipcity,orders.shipcountry,orderdetails.unitprice,orderdetails.quantity,(orderdetails.unitprice*orderdetails.quantity )as total from orders inner join orderdetails on orders.orderid=orderdetails.orderid
select orderid,customerid,sum(total) from Purchase_by_each_customer group by customerid
Error message is Column 'Purchase_by_each_customer.orderid' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Can aggregate functions be used with views,if so how?
how to use Group by in multiple table joins?
Plz help me