MySQL组合特定字段
这与相关,但在格式和执行方面有所不同(显然)。我试着使用那里的信息,但没有任何逻辑结果 案例:该公司有多个部门。我们想知道我们在哪里赚钱 我们的桌子看起来像这样-MySQL组合特定字段,mysql,Mysql,这与相关,但在格式和执行方面有所不同(显然)。我试着使用那里的信息,但没有任何逻辑结果 案例:该公司有多个部门。我们想知道我们在哪里赚钱 我们的桌子看起来像这样- Date | Total | Item Description ----------------------------------- 12122012 1000 Butter cookies 12122012 600 Chocolate Coins 12122012 1500 Candy
Date | Total | Item Description
-----------------------------------
12122012 1000 Butter cookies
12122012 600 Chocolate Coins
12122012 1500 Candy Canes
12142012 1300 Apple Pie
12142012 1300 Pumpkin Pie
12142012 900 Chocolate Cookies
我想保留的格式完全一样,但分组的糖果,饼干和馅饼的产品。我们的代码如下所示:
SELECT Payments_ReceivedDate AS date, CAST(sum(Payments_Amount) AS UNSIGNED ) as total,
tblTreats.Item_Description as 'Item Description'
FROM tblPayments
INNER JOIN tblTreats ON tblPayments.Items_Ordered = tblTreats.Treats_PK
WHERE Payments_ReceivedDate BETWEEN '20120101' AND '20140101'
GROUP BY Items_Ordered
任何帮助都将不胜感激。重要的是,这使用了与其他示例不同的表。它限制了“颜色”和“计数”的使用;将其替换为“项目描述”和“总计”。问题是,通过使用这些信息进行操作,它会将表更改为缺少大量信息的输出。它最终使
糖果计数
,饼干计数
,和馅饼计数
成为一个字段的总数;我错了,我要用正确的栏编辑答案。
-- use a subquery
select sum(case when Item_Description in ('Candy Canes') then total end) as Candies_total,
sum(case when Item_Description in ('Butter cookies', 'Butter cookies','Chocolate Cookies') then total end) as Cookies_total,
sum(case when Item_Description in ('Apple Pie','Pumpkin Pie') then total end) as Pies_total
from (SELECT Payments_ReceivedDate AS date, CAST(sum(Payments_Amount) AS UNSIGNED ) as total,
tblTreats.Item_Description as Item_Description
FROM tblPayments
INNER JOIN tblTreats ON tblPayments.Items_Ordered = tblTreats.Treats_PK
WHERE Payments_ReceivedDate BETWEEN '20120101' AND '20140101'
GROUP BY Items_Ordered) AS t