2012-09-19 162 views
0

这是我的查询:丰富表

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的帐户:暂定,取消,关闭,暂停开放。

我想我的结果表包含aditional的三列: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
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 
+0

完美!谢谢 :) – Dantes