将子查询包含在SQL CASE语句中

时间:2015-10-21 07:29:31

标签: mysql

以下查询用于从ALM获取阻止的测试用例数。是否可以在CASE语句中包含此查询,以使CASE语句的输出为“COUNT(LINK.LN_ENTITY_ID)”?

SELECT  BUG.BG_BUG_ID, COUNT(LINK.LN_ENTITY_ID) AS COUNT_TC, TESTCYCL.TC_STATUS FROM BUG
LEFT JOIN ALL_LISTS
ON BG_SUBJECT = AL_ITEM_ID
INNER JOIN LINK
ON BG_BUG_ID = LN_BUG_ID
LEFT JOIN TESTCYCL
ON TESTCYCL.TC_TEST_ID = LINK.LN_ENTITY_ID AND LINK.LN_ENTITY_TYPE = 'TESTCYCL'
WHERE TESTCYCL.TC_STATUS = 'Blocked'
GROUP BY BG_BUG_ID, TESTCYCL.TC_STATUS

1 个答案:

答案 0 :(得分:0)

SELECT  BUG.BG_BUG_ID,
       SUM(CASE TESTCYCL.TC_STATUS WHEN ="Blocked" THEN 1 ELSE 0 END) AS COUNT_TC,
       TESTCYCL.TC_STATUS
  FROM BUG
    LEFT JOIN ALL_LISTS
    ON BG_SUBJECT = AL_ITEM_ID
    INNER JOIN LINK
    ON BG_BUG_ID = LN_BUG_ID
    LEFT JOIN TESTCYCL
    ON TESTCYCL.TC_TEST_ID = LINK.LN_ENTITY_ID AND LINK.LN_ENTITY_TYPE = 'TESTCYCL'
    GROUP BY BG_BUG_ID

此查询将显示总数。每个"blocked"的状态为BUG_ID的错误。(假设您需要)。

希望这有帮助