我有一个表,我想选择一些行并按日期对它们进行分组。问题是when i'm selecting within my select that row appear to not grouped by date
,请参阅示例:
SELECT date, SUM(price), (SELECT SUM(price), date FROM expense WHERE
user = 'Catherine') as cprice FROM expense GROUP BY date
它没有对cprice
进行分组,它会将所有凯瑟琳价格加起来并且不按日期对其进行分组,顺便说一下,我将GROUP BY日期考虑到内部选择但不起作用。任何建议?
expense table:
id user price date
1 Frank 20 2014
2 Catherine 10 2013
3 Catherine 20 2014
谢谢
答案 0 :(得分:2)
你可以试试这个:
SELECT
date,
SUM(price) as totalPrice,
SUM(IF(user='Catherine', price, 0)) as cprice
FROM expense
GROUP BY date
如果你想要两个价格在一起,那么你可以试试这个:
SELECT
date,
SUM(price) + SUM(IF(user='Catherine', price, 0)) as cprice
FROM expense
GROUP BY date
答案 1 :(得分:0)
尝试此查询,选择*行,其中user =' Catherine'并按日期对它们进行分组
SELECT
date,
SUM(price)
FROM expense
WHERE user = 'Catherine'
GROUP BY date
答案 2 :(得分:0)
尝试使用Case Statement
:( 我不熟悉MySQL:()
不确定,喜欢:
SELECT
date,
SUM(price) as totalPrice,
SUM(Case When user='Catherine' Then price Else 0 End CASE;) as catherine_price
FROM expense
GROUP BY date
答案 3 :(得分:0)
你可能可以使用两个选择来解决它,并使用循环拉出你需要的数据。