簡體   English   中英

SQL 條件重復時的情況

[英]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;

演示: http://sqlfiddle.com/#!17/9eecb/101807

你想連接字符串。 由於缺少一些 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM