【问题标题】:Pivot Table from Query involving Months in TeraData查询中涉及 TeraData 中月份的数据透视表
【发布时间】:2021-02-05 23:57:06
【问题描述】:

我有一个从 TeraData 中的查询生成的表,如下所示:

User_ID     Transaction_Month      Number_of_Visits
 123              1                       1
 123              2                       2
 221              4                       1
 123              5                       2
 221              3                       5

我正在尝试创建一个如下所示的数据透视表:

User_ID     Month_1    Month_2    Month_3  Month_4 ..... Month_12
 123         

这将存储每个月列中的访问次数。

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: sql count pivot teradata aggregate-functions


    【解决方案1】:

    你可以做条件聚合:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多