我有一个名为Ordhdr的表,我想基于该表的[status]列创建查询。我想将许多状态分为“赢得”,“丢失”,“待处理”,以便于查看。如何设置我的声明,设置语句以首先将当前状态“重命名”为WON
,LOST
或PENDING
,以便随后可以使用新名称显示查询结果。>
Sample Data
Order 1, STatus is QteCall1 (Pending Status)
Order 2, Status is Converted (Won Status)
Order 3, STatus is Declined (Dead Status)
Order 4, Status is Waiting (Pending Status)
Order 5, Status is NOResponse (Dead Status)'
我如何“重命名”当前状态以获得结果 显示为
答案 0 :(得分:1)
这是您需要的吗?
SELECT CASE WHEN STATUS = 'Converted' THEN 'WON'
WHEN STATUS IN ('QteCall1','Waiting') THEN 'PENDING'
WHEN STATUS IN ('Declined', 'NOResponse ') THEN 'LOST' END AS NEW_NAME
--The above case expression is being used to compare and rename the values according to the desired match
,COUNT(*) AS COUNT
FROM Ordhdr
GROUP BY
CASE WHEN STATUS = 'Converted' THEN 'WON'
WHEN STATUS IN ('QteCall1','Waiting') THEN 'PENDING'
WHEN STATUS IN ('Declined', 'NOResponse ') THEN 'LOST' END
-- This second case expression is the same as the previous one, but it is required by the SQL standards to be in the GROUP BY section