我有以下数据
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');
我想创建一个新的列,显示拥有simple的客户,然后是simple from
Status_Col
,如果他们有其他,那么其他。但是,如果他们在任何阶段都有simple或other的Status\u Col列,那么我想要
NewStatus
Other/Simple
.
In 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