SQL - 使用 WHEN THEN ELSE 语句时,如何删除 ELSE 行?

问题描述 投票:0回答:1

我有 3 个条件需要寻找批准日期,所有条件都在 WHEN THEN 语句中说明。我在末尾使用 ELSE,因为我知道还有一些尚未获得批准,并将它们命名为“N/A”而不是日期。当我导出此列表时,我只需要批准日期的 3 个条件,而不需要那些不适用的条件。我怎样才能删除那些 ELSE 结果或者有更好的方法来做到这一点?

感谢您的帮助。

CASE
WHEN
(SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%Ready for QA Approval</font></b> to <b><font color = "green">Final Approved%') LIKE '%[0-9]%'
THEN (SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%Ready for QA Approval</font></b> to <b><font color = "green">Final Approved%')
WHEN
(SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%Ready for Approval</font></b> to <b><font color = "green">Final Approved%') LIKE '%[0-9]%'
THEN (SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%Ready for Approval</font></b> to <b><font color = "green">Final Approved%')
WHEN
(SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%In Approval</font></b> to <b><font color = "green">Final Approved%') LIKE '%[0-9]%'
THEN (SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%In Approval</font></b> to <b><font color = "green">Final Approved%')
ELSE 'N/A'
END AS "Approval Date"

我是 SQL 新手,所以到目前为止我还没有尝试过太多。

sql case
1个回答
0
投票

重新整理我的 SQL 知识- 我希望这有帮助!!将“YourTable”替换为您的

如果您想从输出中排除“N/A”结果,您可以在 SQL 查询中添加 WHERE 子句来过滤掉这些结果。以下是修改查询的方法:

  SELECT *
FROM (
    SELECT 
        CASE
            WHEN
                (SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%Ready for QA Approval</font></b> to <b><font color = "green">Final Approved%') LIKE '%[0-9]%'
            THEN (SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%Ready for QA Approval</font></b> to <b><font color = "green">Final Approved%')
            WHEN
                (SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%Ready for Approval</font></b> to <b><font color = "green">Final Approved%') LIKE '%[0-9]%'
            THEN (SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%Ready for Approval</font></b> to <b><font color = "green">Final Approved%')
            WHEN
                (SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%In Approval</font></b> to <b><font color = "green">Final Approved%') LIKE '%[0-9]%'
            THEN (SELECT Substring(cy.cy_user_07,Charindex('Final Approved',cy.cy_user_07)+25,10) FROM cycle CY WHERE cy.cy_cycle_id = c.cy_cycle_id AND cy.cy_user_07 LIKE '%In Approval</font></b> to <b><font color = "green">Final Approved%')
            ELSE 'N/A'
        END AS "Approval Date"
    FROM YourTable
) AS T
WHERE T."Approval Date" != 'N/A'
© www.soinside.com 2019 - 2024. All rights reserved.