SQL - 仅用case语句分组

时间:2017-10-02 09:25:28

标签: sql group-by case

我有以下代码,我只想通过New_Channel对其进行分组。我想要11行(a-j和未知)和计数。

select count(transaction_number) [transactions]
,case when Sales_Location = 4235                                                   then 'a. Online Sale - Fast Track - Store Pick Up'
                    when SALES_LOCATION = 678 and FULFILLMENT_METHOD = 5                              then 'b. Online Sale - Fast Track - Home Delivery'   
            when SALES_LOCATION in ('678','5')                                                then 'c. Online Sale - Home Delivery'
            when FULFILLMENT_METHOD = 10 and CATEGORY = 2                                     then 'd. Online Order - Check & Reserve - Store Pick Up'
            when Till_Number In (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) 
                      and FULFILLMENT_METHOD = 10                                             then 'e. Store Sale - Till Payment - Store Pick Up'
            when TILL_NUMBER In (30,31,32,33,34,35,36,37,38,39) and FULFILLMENT_METHOD = 10   then 'f. Store Sale - Kiosk Payment - Store Pick Up '
            when SALES_LOCATION = 500 AND TILL_NUMBER BETWEEN 60 AND 69                       then 'g. Phone Sale - Home Delivery'
            when SALES_LOCATION IN (5,678) AND TILL_NUMBER BETWEEN 40 AND 49                  then 'g. Phone Sale - Home Delivery'
            when SALES_LOCATION = 500 AND TILL_NUMBER = 90                                    then 'g. Phone Sale - Home Delivery'
            when SALES_LOCATION = 500                                                         then 'c. Online Sale - Home Delivery'
            when FULFILLMENT_METHOD = 10                                                      then 'h. Store Sale - Store Pick Up'
            when FULFILLMENT_METHOD = 05                                                      then 'i. Store Sale - Fast Track Home Delivery'
            when FULFILLMENT_METHOD in ('02','07','06')                                       then 'j. Store Sale - Home Delivery'
            else 'Unknown' end as New_Channel
from HRG_PROD.PRD.SKU_SALES_HISTORY_2017
group by Sales_Location, Fulfillment_Method, Category, Till_Number

1 个答案:

答案 0 :(得分:0)

我已经解决了。使用派生表工作。谢谢jarlh

SELECT nc.New_Channel
,sum(transactions) as [Retail Sales]
from (select
count(Transaction_Number) as [transactions] 
,case when Sales_Location = 4235                                                   then 'a. Online Sale - Fast Track - Store Pick Up'
                    when SALES_LOCATION = 678 and FULFILLMENT_METHOD = 5                              then 'b. Online Sale - Fast Track - Home Delivery'   
            when SALES_LOCATION in ('678','5')                                                then 'c. Online Sale - Home Delivery'
            when FULFILLMENT_METHOD = 10 and CATEGORY = 2                                     then 'd. Online Order - Check & Reserve - Store Pick Up'
            when Till_Number In (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) 
                      and FULFILLMENT_METHOD = 10                                             then 'e. Store Sale - Till Payment - Store Pick Up'
            when TILL_NUMBER In (30,31,32,33,34,35,36,37,38,39) and FULFILLMENT_METHOD = 10   then 'f. Store Sale - Kiosk Payment - Store Pick Up '
            when SALES_LOCATION = 500 AND TILL_NUMBER BETWEEN 60 AND 69                       then 'g. Phone Sale - Home Delivery'
            when SALES_LOCATION IN (5,678) AND TILL_NUMBER BETWEEN 40 AND 49                  then 'g. Phone Sale - Home Delivery'
            when SALES_LOCATION = 500 AND TILL_NUMBER = 90                                    then 'g. Phone Sale - Home Delivery'
            when SALES_LOCATION = 500                                                         then 'c. Online Sale - Home Delivery'
            when FULFILLMENT_METHOD = 10                                                      then 'h. Store Sale - Store Pick Up'
            when FULFILLMENT_METHOD = 05                                                      then 'i. Store Sale - Fast Track Home Delivery'
            when FULFILLMENT_METHOD in ('02','07','06')                                       then 'j. Store Sale - Home Delivery'
            else 'Unknown' end as New_Channel
from HRG_PROD.PRD.SKU_SALES_HISTORY_2014
group by Sales_Location, Fulfillment_Method, Category, Till_Number ) as nc
group by nc.New_Channel