x
login about faq Site discussion (meta-askssc)

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

(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
313 12 21 25

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.

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



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x321

asked: May 21 '12 at 03:15 AM

Seen: 229 times

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

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.