x

Query to find the products and the total price of the each product of a customer

I have a table with the below columns: Customer_Id, Product_Id, Product_Name, Unit_Price, Quantity

I need to find the number of distinct products the customer has purchased and the total cost of the products (which is Unit_Price * quantity). I should get the below 3 columns as the result:

Customer_Id Product_Id Total_Price
more ▼

asked May 21 '12 at 03:15 AM in Default

Shanthi gravatar image

Shanthi
0 1 1

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first
you can use SUM Product_Id and GROUP BY Product_Id to find a total amount of product and than multiply with Unit_price, that's all you have to do.
more ▼

answered May 21 '12 at 08:52 AM

Gogolo gravatar image

Gogolo
323 19 26 27

But I also need the customer_Id column. When I use "group by", I cannot select customer_Id as a column. Please help...
May 21 '12 at 11:56 AM Shanthi
Yes you can, but you have to GROUP BY also customer_Id
May 21 '12 at 12:43 PM Gogolo
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x362

asked: May 21 '12 at 03:15 AM

Seen: 408 times

Last Updated: May 21 '12 at 01:20 PM