根据条件在同一列中的条件创建新的列状态

时间:2018-10-02 12:12:43

标签: sql-server tsql sql-server-2008

我有以下数据

DECLARE @TBL TABLE (CustomerID INT, CustomerName VARCHAR(100),Product VARCHAR(100), Status_Col VARCHAR(100))
-- INSERT DATA
INSERT INTO @TBL VALUES     (90,'Wilman Kala','Dairy','Simple'),
    (90,'Wilman Kala','Grain','Other'), (90,'Wilman Kala','Dairy','Other'), (81,'Hipermercados','Produce','Simple'),
    (81,'Hipermercados','Produce','Other'), (34,'Hanari Carnes','Seafood','Simple'),(34,'Hanari Carnes','Produce','Simple'),
    (34,'Hanari Carnes','Condiments','Simple'), (85,'Smith stock','Grains','Other'),    (86,'Jane will','Cereals','Other'),
    (87,'Victuailles en stock','Condiments','Simple'),  (88,'Suprêmes délices','Confections','Other'),
    (88,'Suprêmes délices','Dairy','Other'),    (89,'Marry Dawson','Dairy','Simple'),(90,'Eve Lawson','Dairy','Other'),
    (91,'Hanari Carnes','Beverages','Other'),   (91,'Hanari Carnes','Confections','Other'),
    (92,'Mark Johnson','Beverages','Other');

我想创建一个新列,该列将显示Status_Col中具有“简单”然后“简单”的客户,如果他们具有“其他”然后是“其他”。但是,如果他们在状态Status_Col栏的任何阶段都具有“简单”或“其他”,那么我希望NewStatus向其显示Other/SimpleIn my data CustomerID 81 and 90 should show其他/简单

我尝试了以下查询,但没有给出正确答案

SELECT CustomerID,CustomerName,Product,
Status_Col,
CASE WHEN Status_Col='Other' THEN 'Other'
     WHEN Status_Col='Simple' THEN 'Simple'
     WHEN Status_Col='Other' OR Status_Col='Simple' THEN 'Other/Simple' END AS NewStatus
FROM @TBL
order by CustomerName

当前输出

CustomerID  CustomerName            Product         Status_Col  NewStatus
90           Eve Lawson             Dairy              Other      Other
91           Hanari Carnes          Beverages          Other      Other
91           Hanari Carnes          Confections        Other      Other
34           Hanari Carnes          Seafood            Simple     Simple
34           Hanari Carnes          Produce            Simple     Simple
34           Hanari Carnes          Condiments         Simple     Simple
81           Hipermercados          Produce            Simple     Simple
81           Hipermercados          Produce            Other      Other
86           Jane will              Cereals            Other      Other
92           Mark Johnson           Beverages          Other      Other
89           Marry Dawson           Dairy              Simple     Simple
85           Smith stock            Grains             Other      Other
88           Suprêmes délices       Confections        Other      Other
88           Suprêmes délices       Dairy              Other      Other
87           Victuailles en stock   Condiments         Simple     Simple
90           Wilman Kala            Dairy              Simple     Simple
90           Wilman Kala            Grain              Other      Other
90           Wilman Kala            Dairy              Other      Other

预期产量

CustomerID  CustomerName            Product         Status_Col  NewStatus
90           Eve Lawson             Dairy              Other      Other
91           Hanari Carnes          Beverages          Other      Other
91           Hanari Carnes          Confections        Other      Other
34           Hanari Carnes          Seafood            Simple     Simple
34           Hanari Carnes          Produce            Simple     Simple
34           Hanari Carnes          Condiments         Simple     Simple
81           Hipermercados          Produce            Simple   Other/Simple
81           Hipermercados          Produce            Other    Other/Simple
86           Jane will              Cereals            Other      Other
92           Mark Johnson           Beverages          Other      Other
89           Marry Dawson           Dairy              Simple     Simple
85           Smith stock            Grains             Other      Other
88           Suprêmes délices       Confections        Other      Other
88           Suprêmes délices       Dairy              Other      Other
87           Victuailles en stock   Condiments         Simple     Simple
90           Wilman Kala            Dairy              Simple   Other/Simple
90           Wilman Kala            Grain              Other    Other/Simple
90           Wilman Kala            Dairy              Other    Other/Simple

2 个答案:

答案 0 :(得分:1)

您的CASE表达式没有什么意义(最终表达式将永远都不是正确的,因为先前的表达式之一已经必须是真实的)。似乎您正在尝试检查其他行的值,而不是整个引用数据集。

这似乎是您想要的答案:

SELECT CustomerID,
       CustomerName,
       Product,
       Status_Col,
       CASE WHEN COUNT(CASE Status_Col WHEN 'Simple' THEN 1 END) OVER (PARTITION BY CustomerID, CustomerName) > 0
             AND COUNT(CASE Status_Col WHEN 'Other' THEN 1 END) OVER (PARTITION BY CustomerID, CustomerName) > 0 THEN 'Other/Simple'
            ELSE Status_Col --If both values don't appear, then it must be the current one
        END AS NewStatus
FROM @TBL
order by CustomerName;

答案 1 :(得分:1)

您可以使用相关子查询:

SELECT CustomerID,CustomerName,Product,
Status_Col,
CASE WHEN (
            SELECT COUNT(DISTINCT Status_Col) 
            FROM @TBL AS T1 
            WHERE T0.CustomerID = T1.CustomerID 
            AND T0.CustomerName = T1.CustomerName
           ) > 1 THEN 'Other/Simple'
     WHEN Status_Col='Other' THEN 'Other'
     WHEN Status_Col='Simple' THEN 'Simple' END AS NewStatus
FROM @TBL AS T0
order by CustomerName

不过,我想知道为什么您为同一个customerId有多个客户名称。