0

I want to execute a subquery using the current customer ID as I try to describe below

SELECT DISTINCT  Customer_Id, 
(SELECT SUM  (total) FROM Orders where Customer_Id = Customer_Id AND CAST(Date) > DayIspecify )  
FROM Orders  where shop_id= '1-9THT' 

What I want is to calculate the SUM each customer spent over a specified time period on the specific shop.

7
  • What version of SQL server are you using? Commented Apr 24, 2013 at 10:36
  • you don't want a subquery but group by Commented Apr 24, 2013 at 10:36
  • Do you want the value of orders placed at shop 1-9THT, broken down by customer, or do you want customers with the values of all their orders, where any of their orders was placed through shop 1-9THT? Commented Apr 24, 2013 at 10:51
  • Sorry let me rephrase my question. I want to select the SUM over a period of time that I will specify Commented Apr 24, 2013 at 10:52
  • OK - do you have a separate customer table? Commented Apr 24, 2013 at 11:09

4 Answers 4

4
SELECT Customer_Id, SUM(total) SumTotal
FROM Orders
where shop_id= '1-9THT' 
group by Customer_id
Sign up to request clarification or add additional context in comments.

Comments

2

Not require subquery Try this:

SELECT Customer_Id,SUM(total)FROM Orders WHERE shop_id='1-9THT' GROUP BY Customer_Id

Comments

1

(Updated) Try:

select Customer_Id, 
       sum(case when o.shop_id = '1-9THT' and Date > DayIspecify 
                then total else 0 end) total
from Orders
group by Customer_Id

- to return all customers recorded on the Orders table, together with the values of any of their orders placed through shop 1-9THT after the date specified. (Change > to >= to make it on or after the date specified.)

Comments

0

Use SQL GroupBy

SELECT DISTINCT  Customer_Id,  SUM  (total) FROM Orders  where shop_id= '1-9THT' group by customer_Id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.