0

我有一个名为 Ordhdr 的表,我想根据该表的 [status] 列创建一个查询。我有许多状态,我想将它们分组为 Won、Lost、Pending 以便于查看。如何设置我的声明,设置语句首先将当前状态“重命名”为WONLOST或者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)'

我将如何“重命名”当前状态以获得显示计数的结果

  • 1 赢
  • 2 待定
  • 2 死
4

1 回答 1

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

查询结果。

于 2019-01-25T18:42:59.170 回答