[英]How to simplify a sql with a lot of “case when then else …”
[英]SQL Case When Repeating Conditions
我有一个名为 country 的表,它有 2 列: orderid, country_code 。
我需要在以下情况下执行以下操作:
SELECT
CASE
WHEN country_code IN ('PT','IT','ES','PL','AD') THEN 'SWE'
WHEN country_code IN ('MD','BG','BA','SI','HR','ME','RO','RS') THEN 'SEE'
WHEN country_code IN ('CI','GH','MA','NG','UG','KE','TN') THEN 'AFRICA'
WHEN country_code IN ('CI','GH','NG','UG','KE','TN') THEN 'SSA'
WHEN country_code IN ('UA','BY','GE','KZ','KG','AM') THEN 'ECA'
END AS region,
COUNT(DISTINCT orderid) AS amount_of_orders
FROM country
GROUP BY 1
但是,当我运行代码时,区域 SSA 没有出现,因为 (AFRICA) 之前的条件使用的是 SSA 的国家(SSA 与 AFRICA 相同,但没有 MA) 。
我怎样才能获得完整的非洲和 SSA 订单量?
关系数据库:亚马逊红移
编辑:这是我现在的桌子:
SWE 200
SEE 500
AFRICA 350
SSA 0 <--- (it doesn't appear because the conditions were met by AFRICA region)
ECA 200
我需要以下内容:
SWE 200
SEE 500
AFRICA 350 --> (MA represent 150 orders)
SSA 200 --> (sames as AFRICA, but without MA)
ECA 200
您可以定义一个包含所有国家代码及其对应区域的公用表表达式 (CTE),然后将其与您的数据连接起来。
WITH regions AS
(
SELECT CAST('PT,IT,ES,PL,AD' AS VARCHAR) AS country_codes, 'SWE' AS region_code
UNION ALL SELECT CAST('MD,BG,BA,SI,HR,ME,RO,RS' AS VARCHAR) AS country_codes, 'SEE' AS region_code
UNION ALL SELECT CAST('CI,GH,MA,NG,UG,KE,TN' AS VARCHAR) AS country_codes, 'AFRICA' AS region_code
UNION ALL SELECT CAST('CI,GH,NG,UG,KE,TN' AS VARCHAR) AS country_codes, 'SSA' AS region_code
UNION ALL SELECT CAST('UA,BY,GE,KZ,KG,AM' AS VARCHAR) AS country_codes, 'ECA' AS region_code
)
SELECT
regions.region_code AS region,
COUNT(DISTINCT orderid) AS amount_of_orders
FROM country
INNER JOIN regions ON STRPOS(regions.country_codes, country.country_code) > 0
GROUP BY regions.region_code;
你想连接字符串。 由于缺少一些 CONCAT_WS function 跳过空值,只需在每个区域使用逗号并在最后相应地修剪字符串。
SELECT
TRIM(',' FROM
CASE WHEN country_code IN ('PT','IT','ES','PL','AD') THEN ',SWE' ELSE '' END ||
CASE WHEN country_code IN ('MD','BG','BA','SI','HR','ME','RO','RS') THEN ',SEE' ELSE '' END ||
CASE WHEN country_code IN ('CI','GH','MA','NG','UG','KE','TN') THEN ',AFRICA' ELSE '' END ||
CASE WHEN country_code IN ('CI','GH','NG','UG','KE','TN') THEN ',SSA' ELSE '' END ||
CASE WHEN country_code IN ('UA','BY','GE','KZ','KG','AM') THEN ',ECA' ELSE '' END
) AS regions,
COUNT(DISTINCT orderid) AS amount_of_orders
FROM country
GROUP BY regions
ORDER BY regions;
您已完全更改您的请求。 您回答说您想要一个包含区域列表的列(例如“AFRICA,SSA”),但现在您表明您想要每个区域一行。 请不要对您的请求进行此类根本性更改。 但是,您现在想要一个单一区域查询的联合:
SELECT 'SWE' AS region, COUNT(DISTINCT orderid) AS amount_of_orders
FROM country
WHERE country_code IN ('PT','IT','ES','PL','AD')
UNION ALL
SELECT 'SEE' AS region, COUNT(DISTINCT orderid) AS amount_of_orders
FROM country
WHERE country_code IN ('MD','BG','BA','SI','HR','ME','RO','RS')
UNION ALL
SELECT 'AFRICA' AS region, COUNT(DISTINCT orderid) AS amount_of_orders
FROM country
WHERE country_code IN ('CI','GH','MA','NG','UG','KE','TN')
UNION ALL
SELECT 'SSA' AS region, COUNT(DISTINCT orderid) AS amount_of_orders
FROM country
WHERE country_code IN ('CI','GH','NG','UG','KE','TN')
UNION ALL
SELECT 'ECA' AS region, COUNT(DISTINCT orderid) AS amount_of_orders
FROM country
WHERE country_code IN ('UA','BY','GE','KZ','KG','AM');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.