丰富的表

时间:2012-09-19 09:01:27

标签: sql teradata

这是我的疑问:

 SELECT 
 a.account_type AS ACCOUNT_TYPE
,b.at_account_type_desc
,COUNT(a.BAN) AS num_BAN 
 FROM csm_adx.billing_account_act AS a 
 LEFT OUTER JOIN csm_adx.account_type_act AS b ON a.account_type = b.at_acc_type
 GROUP BY 1,2

现在我想将它连接到另一个表TABLE_C,其中包含的信息是帐户:暂定,取消,关闭,暂停,打开。

我希望我的结果表包含附加的三列:ACTIVE_BANSUSPENDED_BANCANCELLED_BAN 并且每个值包含当前活动,暂停和取消禁止的数量。我正在使用Teradata。

你能帮我这么做吗?

当表与另一个包含BAN状态的表连接时,这是结果:

SELECT 
 a.account_type AS ACCOUNT_TYPE
,b.at_account_type_desc
,c.description
,COUNT(a.BAN) AS num_BAN
FROM csm_adx.billing_account_act AS a
LEFT OUTER JOIN csm_adx.account_type_act AS b 
ON a.account_type = b.at_acc_type
LEFT OUTER JOIN csm_adx.acct_status AS c
ON a.ban_status = c.original_status_code
GROUP BY 1,2,3

1 个答案:

答案 0 :(得分:1)

SELECT 
 a.account_type AS ACCOUNT_TYPE
,b.at_account_type_desc
,COUNT(a.BAN) AS num_BAN ,
 sum(case when a.column=value then 1 else 0 end) as 'user_colname1',
sum(case when b.column=value then 1 else 0 end) as 'user_colname2'
 FROM csm_adx.billing_account_act AS a 
 LEFT OUTER JOIN csm_adx.account_type_act AS b 
ON a.account_type = b.at_acc_type
 GROUP BY 1,2