我正在编写一个sql报告,对我的要求之一是为CASE表达式的两个选项都包含行。
该报告将显示数据库中所有客户与3个所选客户中的每个客户之间的“费用”和“非成本”交易的计数。
请参阅所需的示例布局。
CUSTOMER_ID | CUSTOMER_NAME | COSTING | CUST_1 | CUST_2 | CUST_3
12 | Red Corp | Non-costing | 1245 | 734 | 84
12 | Red Corp | Costing | 376 | 23 | 2
17 | Blue Corp | Non-costing | 4538 | 36 | 3256
17 | Blue Corp | Costing | 0 | 0 | 0
...
我尝试将COSTING列放在case语句中。但是,由于空白,我无法在输出中显示示例“ Blue Corp”“ Costing”行。
select CUSTOMER_ID
,CUSTOMER_NAME
,case
when PURCHASE_VALUE = 0 or PURCHASE_VALUE is null then 'Non-costing'
when PURCHASE_VALUE != 0 or PURCHASE_VALUE is not null then 'Costing'
end AS COSTING
,count(CASE WHEN CUSTOMER_ID = 11 then 1 ELSE NULL END) as "CUST_1"
,count(CASE WHEN CUSTOMER_ID = 22 then 1 ELSE NULL END) as "CUST_2"
,count(CASE WHEN CUSTOMER_ID = 33 then 1 ELSE NULL END) as "CUST_3"
from schema.CUSTOMER_TRANSACTIONS
group by case
when PURCHASE_VALUE = 0 or PURCHASE_VALUE is null then 'Non-costing'
when PURCHASE_VALUE != 0 or PURCHASE_VALUE is not null then 'Costing'
end
,CUSTOMER_ID
,CUSTOMER_NAME
我目前与示例相同,但是缺少0计数行。 请在下面查看我当前得到的输出。
CUSTOMER_ID | CUSTOMER_NAME | COSTING | CUST_1 | CUST_2 | CUST_3
12 | Red Corp | Non-costing | 1245 | 734 | 84
12 | Red Corp | Costing | 376 | 23 | 2
17 | Blue Corp | Non-costing | 4538 | 36 | 3256
...
答案 0 :(得分:2)
使用cross join
生成行,并使用left join
添加列:
select c.CUSTOMER_ID, c.CUSTOMER_NAME, t.costing,
sum(CASE WHEN CUSTOMER_ID = 11 then 1 ELSE 0 END) as CUST_1,
sum(CASE WHEN CUSTOMER_ID = 22 then 1 ELSE 0 END) as CUST_2,
sum(CASE WHEN CUSTOMER_ID = 33 then 1 ELSE 0 END) as CUST_3
from (select distinct ct.customer_id, ct.customer_name
from schema.CUSTOMER_TRANSACTIONS ct
) c cross join
(select 'Non-costing' as costing from dual union all
select 'Costing' from dual
) t left join
schema.customer_transactions ct
on c.customer_id = ct.customer_id,
t.costing = (case when PURCHASE_VALUE = 0 or PURCHASE_VALUE is null
then 'Non-costing'
else 'Costing'
end)
group by c.CUSTOMER_ID, c.CUSTOMER_NAME, t.type;
答案 1 :(得分:1)
根据您的评论
Alex Poole,您是正确的,没有用于“ Costing”的数据 “ Blue-corp”。但是我希望该行显示为0而不是 完全出现。
我认为您需要生成一个记录,其中没有成本计算之一。
我试图创建查询,如果“成本”或“非成本”记录不适用于任何客户,则将生成记录。
我无法对其进行测试,因此如果您遇到任何问题,请分享。
-- Your original query starts from here
WITH DATAA AS (select CUSTOMER_ID
,CUSTOMER_NAME
,case
when PURCHASE_VALUE = 0 or PURCHASE_VALUE is null then 'Non-costing'
when PURCHASE_VALUE != 0 or PURCHASE_VALUE is not null then 'Costng'
end AS COSTING
,count(CASE WHEN CUSTOMER_ID = 11 then 1 ELSE NULL END) as "CUST_1"
,count(CASE WHEN CUSTOMER_ID = 22 then 1 ELSE NULL END) as "CUST_2"
,count(CASE WHEN CUSTOMER_ID = 33 then 1 ELSE NULL END) as "CUST_3"
from schema.CUSTOMER_TRANSACTIONS
group by case
when PURCHASE_VALUE = 0 or PURCHASE_VALUE is null then 'Non-costing'
when PURCHASE_VALUE != 0 or PURCHASE_VALUE is not null then 'Costing'
end
,CUSTOMER_ID
,CUSTOMER_NAME)
-- Your original query ends here
-- Solution
SELECT
CUSTOMER_ID,
CUSTOMER_NAME,
COSTING,
CUST_1,
CUST_2,
CUST_3
FROM
DATAA
UNION ALL
( SELECT
CUSTOMER_ID,
CUSTOMER_NAME,
CASE
WHEN COUNT(CASE
WHEN COSTING = 'Non-costing' THEN 1
END) = 0 THEN 'Non-costing'
WHEN COUNT(CASE
WHEN COSTING = 'Costing' THEN 1
END) = 0 THEN 'Costing'
END AS COSTING,
0,
0,
0
FROM
DATAA D1
GROUP BY
CUSTOMER_ID,
CUSTOMER_NAME
HAVING ( COUNT(CASE
WHEN COSTING = 'Non-costing' THEN 1
END) = 0
OR COUNT(CASE
WHEN COSTING = 'Costng' THEN 1
END) = 0 )
);
干杯!
答案 2 :(得分:0)
您可以尝试一下。没有时它将给您0。
select CUSTOMER_ID
,CUSTOMER_NAME
, CASE WHEN ISNULL(PURCHASE_Value,0) = 0 THEN 'Non-costing' ELSE 'Costing' END
,SUM(CASE WHEN CUSTOMER_ID = 11 then 1 ELSE 0 END) as "CUST_1"
,SUM(CASE WHEN CUSTOMER_ID = 22 then 1 ELSE 0 END) as "CUST_2"
,SUM(CASE WHEN CUSTOMER_ID = 33 then 1 ELSE 0 END) as "CUST_3"
from schema.CUSTOMER_TRANSACTIONS
group by CUSTOMER_ID
,CUSTOMER_NAME
,PURCHASE_Value