Teradata SQL 中的摘要 table
Summary table in Teradata SQL
我在 Teradata SQL 中有以下 table,称为 transactions_data:
User_ID Trans_Date Amount Purchased
3134 2012-08-12 3.35
3135 2012-08-12 4.47
3134 2012-08-13 4.45
我想做的是创建一个 table,其中列涵盖 table 中的所有日期,行是每个用户当天花费的金额,因此,示例:
User_ID 2012-08-12 2012-08-13 2012-08-14
3134 3.35 4.47 0
3135 4.47 0 0
在 Teradata 中是否有一种有效的方法来执行此操作,特别是如果总共有 365 个日期(即一年的日期或 2 年的日期?)
谢谢。
如果事先知道日期列表,可以使用条件聚合:
select user_id,
max(case when trans_date = '2012-08-12' then amount_purchased end) as amount_2012_08_12,
max(case when trans_date = '2012-08-13' then amount_purchased end) as amount_2012_08_13,
...
from mytable
group by user_id
我在 Teradata SQL 中有以下 table,称为 transactions_data:
User_ID Trans_Date Amount Purchased
3134 2012-08-12 3.35
3135 2012-08-12 4.47
3134 2012-08-13 4.45
我想做的是创建一个 table,其中列涵盖 table 中的所有日期,行是每个用户当天花费的金额,因此,示例:
User_ID 2012-08-12 2012-08-13 2012-08-14
3134 3.35 4.47 0
3135 4.47 0 0
在 Teradata 中是否有一种有效的方法来执行此操作,特别是如果总共有 365 个日期(即一年的日期或 2 年的日期?)
谢谢。
如果事先知道日期列表,可以使用条件聚合:
select user_id,
max(case when trans_date = '2012-08-12' then amount_purchased end) as amount_2012_08_12,
max(case when trans_date = '2012-08-13' then amount_purchased end) as amount_2012_08_13,
...
from mytable
group by user_id