从涉及 TeraData 中月份的查询中透视 Table
Pivot Table from Query involving Months in TeraData
我有一个 table 从 TeraData 中的查询生成的,如下所示:
User_ID Transaction_Month Number_of_Visits
123 1 1
123 2 2
221 4 1
123 5 2
221 3 5
我正在尝试创建一个类似于 table 的枢轴:
User_ID Month_1 Month_2 Month_3 Month_4 ..... Month_12
123
这将在每个月列中存储访问次数。
如有任何帮助,我们将不胜感激。
谢谢。
您可以进行条件聚合:
select user_id,
sum(case when transaction_month = 1 then number_of_visits else 0 end) month_1,
sum(case when transaction_month = 2 then number_of_visits else 0 end) month_2,
...
sum(case when transaction_month = 12 then number_of_visits else 0 end) month_12
from mytable
group by user_id
我有一个 table 从 TeraData 中的查询生成的,如下所示:
User_ID Transaction_Month Number_of_Visits
123 1 1
123 2 2
221 4 1
123 5 2
221 3 5
我正在尝试创建一个类似于 table 的枢轴:
User_ID Month_1 Month_2 Month_3 Month_4 ..... Month_12
123
这将在每个月列中存储访问次数。
如有任何帮助,我们将不胜感激。
谢谢。
您可以进行条件聚合:
select user_id,
sum(case when transaction_month = 1 then number_of_visits else 0 end) month_1,
sum(case when transaction_month = 2 then number_of_visits else 0 end) month_2,
...
sum(case when transaction_month = 12 then number_of_visits else 0 end) month_12
from mytable
group by user_id