我有一个表格,其中包含我需要的所有信息,但格式不同。我想连接字段NAME和ACTIVITY以使其唯一,并按月和年分组我的日期。此查询将始终在2个日期之间进行比较,在我指定MONTH1时执行,实际表示分析的第一个月和分析的第二个月MONTH2。我还需要它们之间的差异百分比。
Example of Raw table:
======================
Name Activity Date
++++++++++++++++++++++++++++++++
Paul VideoGame 2015-02-01
John Play Guitar 2015-03-05
Paul Play Guitar 2015-02-01
Paul Play Guitar 2015-02-15
Paul VideoGame 2015-03-04
John Play Guitar 2015-03-06
Output wanted:
==================
Name+Activity Month1 Month2 Percentage
+++++++++++++++++++++++++++++++++++++++++++++++++++
John+Play Guitar 0 2 +200%
Paul+VideoGame 1 1 0%
Paul+Play Guitar 2 0 -200%
答案 0 :(得分:0)
如果你知道几个月,那么你可以通过条件聚合来做到这一点。我会将name
和activity
放在不同的列中。而且,我不太确定如何计算百分比列:
select name, activity,
sum(month(date) = 2) as month1,
sum(month(date) = 3) as month2,
sum(month(date) = 3) - sum(month(date) = 2)) as diff
from rawtable
group by name, activity;
您可以将name
和activity
作为concat_ws('+', name, activity) if you really want. You can convert the
diff`连接到您想要的格式乘以100。