我将使用输出放入Excel数据透视表。该数据正在处理已经收费或不收费的信用账户。
编辑:如果在数据透视表中检查了费用,我希望totalaccounts列为总帐户数,而不考虑chargeoffdate值。如果取消选中chargeoffs,我希望totoffccounts在chargeoffdate为NULL时计算所有帐户。
到目前为止,这是我的SQL语法:
SELECT
c.brand,
CASE WHEN a.chargeoffdate IS NULL THEN 'No Chargeoffs'
-- Below here should not be only chargeoffs, it should be chargeoffs + the column above ^^^
WHEN a.chargeoffdate IS NOT NULL THEN 'Chargeoffs'
ELSE 'Unknown' END AS chargeoffs,
COUNT(*) AS totalaccounts
FROM accounts
GROUP BY brand, chargeoffs
你可以在我的SQL中看到评论,以了解我的目标,但我无法弄清楚如何实现这一点。
我试过了:
CASE WHEN a.chargeoffdate IS NULL THEN 'No Chargeoffs'
-- Below here should not be only chargeoffs, it should be chargeoffs + the column above ^^^
WHEN (a.chargeoffdate IS NOT NULL OR a.chargeoffdate IS NULL) THEN 'Chargeoffs Included'
ELSE 'Unknown' END AS chargeoffs
但出于某种原因得到与顶级查询相同的结果。感谢。
另一个编辑:输出所需
BRAND 1 | WITH CHARGEOFFS | COUNT(TOTALACCOUNTS)
BRAND 1 | WITHOUT CHARGEOFFS | COUNT(TOTALACCOUNTS)
BRAND 2 | WITH CHARGEOFFS | COUNT(TOTALACCOUNTS)
BRAND 2 | WITHOUT CHARGEOFFS | COUNT(TOTALACCOUNTS)
答案 0 :(得分:0)
更新:
Chargeoffs =所有账户的计数,无论chargeoffdate是否为空
No Chargeoffs = chargeoffdate为空的所有账户的计数(他们没有收费)
SELECT
brand,
count(*) as "Chargeoffs",
sum(CASE WHEN a.chargeoffdate IS NULL THEN 1 ELSE 0 END) as 'No Chargeoffs'
FROM accounts
GROUP BY brand
更新:我累了,我得到了这个长SQL,它接近你想要的东西:
SELECT brand,
tp,
CASE WHEN TP = 1 then sum(cnt) END as 'No Chargeoffs',
sum(cnt) as "Chargeoffs"
FROM(
SELECT
brand,
CASE WHEN a.chargeoffdate IS NULL THEN 1 ELSE 0 END as tp
count(*) as cnt
FROM accounts
GROUP BY brand, CASE WHEN a.chargeoffdate IS NULL THEN 1 ELSE 0 END
GROUP BY brand, tp
答案 1 :(得分:0)
这有点愚蠢,我的解决方案很简单。我刚刚离开了我的方式,并且数据透视表为我添加了它们。
在我创建了2个单独的查询并使用SAS进行一些数据操作以获得我想要的内容后,我发现了这一点。哎哟。