Sql子查询语法

时间:2013-04-24 10:33:24

标签: sql

我想使用当前的客户ID执行子查询,我试着在下面描述

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' 

我想要的是计算每个客户在特定商店的指定时间段内花费的SUM。

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

不需要子查询 试试这个:

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

答案 2 :(得分:1)

(更新)尝试:

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

- 返回订单表中记录的所有客户,以及指定日期后通过商店1-9THT 下达的任何订单的价值。 (将>更改为>=,使其指定日期之后。

答案 3 :(得分:0)

使用SQL GroupBy

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