我有以下mysql查询:
Select id, sum(sales_qty) as 'Todays Sales' from sales
Where date = '2015-03-17'
Group by id
现在在同一个查询中,我想要另一个 sales_qty 的聚合函数,但是具有不同的where子句。在单独的查询中,它看起来像:
Select id, sum(sales_qty) as 'Till Date Sales' from sales
Where date between '2015-03-01' and '2015-03-17'
Group by id
如何在单个查询中获得预期结果?
谢谢......它的工作......现在我手头的东西有点复杂......让我解释一下:
第一次查询:
选择ID,sum(sales_qty)为'今日销售数量',总和(sales_value)为'今日销售价值'来自销售 date =' 2015-03-19' 按ID分组
第二次查询:
选择ID,sum(sales_value)作为'直到销售日期'来自销售 在2015-03-01'之间的日期和' 2015-03-19' 按ID分组
预期输出列(使用单个查询):
id,今日销售数量,今日销售额,截至日期销售额
有可能吗?请帮忙。提前谢谢。
答案 0 :(得分:0)
您必须在子查询中执行此操作:
SELECT
t1.id,
t1.`Todays Sales`,
t2.`Till Date Sales`
FROM
(
Select id, sum(sales_qty) as 'Todays Sales' from sales
Where date = '2015-03-17'
Group by id
)t1
INNER JOIN
(
Select id, sum(sales_qty) as 'Till Date Sales' from sales
Where date between '2015-03-01' and '2015-03-17'
Group by id
)t2 ON
t1.id = t2.id