返回2个计数结果声明

时间:2014-03-27 17:51:49

标签: sql tsql

我有两个单独的查询,用于计算数据库中的异常数。我需要在同一个查询中返回两个结果,如何正确地将它们整合在一起?

SELECT (
    IF EXISTS (SELECT *
        FROM
            exception AS ex
        INNER JOIN
            exceptionDefinition AS ed ON ex.exceptionDefId = ed.exceptionDefId
        WHERE
            ex.customerId='{5B65755C-3B66-434E-AC03-942004E9A27A}'
            AND ex.loanId IS NULL
            AND ex.exceptionState LIKE 'Y'
            AND ex.statusType LIKE 'required'
            AND ed.computationType LIKE 'computed'
        GROUP BY
            ex.customerId,
            ed.computationType,
            ex.exceptionState)
        BEGIN 
            SELECT computedExceptionCount = 1
        END
    ELSE
        BEGIN
            SELECT computedExceptionCount = 0
        END
    ) AS computedExceptionCount,

    (
    IF EXISTS (SELECT *
        FROM
            exception AS ex
        INNER JOIN
            exceptionDefinition AS ed ON ex.exceptionDefId = ed.exceptionDefId
        WHERE
            ex.customerId='{5B65755C-3B66-434E-AC03-942004E9A27A}'
            AND ex.loanId IS NULL
            AND ex.exceptionState LIKE 'Y'
            AND ex.statusType LIKE 'required'
            AND ed.computationType LIKE 'manual'
        GROUP BY
            ex.customerId,
            ed.computationType,
            ex.exceptionState)
        BEGIN 
            SELECT manualExceptionCount = 1
        END
    ELSE
        BEGIN
            SELECT manualExceptionCount = 0
        END
    ) AS manualExceptionCount

我确信这很简单..更多的格式问题比什么

非常感谢提前。

3 个答案:

答案 0 :(得分:2)

使用CASE

SELECT (
    CASE WHEN EXISTS (SELECT *
        FROM
            exception AS ex
        INNER JOIN
            exceptionDefinition AS ed ON ex.exceptionDefId = ed.exceptionDefId
        WHERE
            ex.customerId='{5B65755C-3B66-434E-AC03-942004E9A27A}'
            AND ex.loanId IS NULL
            AND ex.exceptionState LIKE 'Y'
            AND ex.statusType LIKE 'required'
            AND ed.computationType LIKE 'computed'
        GROUP BY
            ex.customerId,
            ed.computationType,
            ex.exceptionState)
    THEN 1
    ELSE 0
    END
    ) AS computedExceptionCount,

    (
    CASE WHEN EXISTS (SELECT *
        FROM
            exception AS ex
        INNER JOIN
            exceptionDefinition AS ed ON ex.exceptionDefId = ed.exceptionDefId
        WHERE
            ex.customerId='{5B65755C-3B66-434E-AC03-942004E9A27A}'
            AND ex.loanId IS NULL
            AND ex.exceptionState LIKE 'Y'
            AND ex.statusType LIKE 'required'
            AND ed.computationType LIKE 'manual'
        GROUP BY
            ex.customerId,
            ed.computationType,
            ex.exceptionState)
    THEN 1
    ELSE 0 
    END        
    ) AS manualExceptionCount

答案 1 :(得分:0)

为什么不在上面声明calculateExceptionCount和manualExceptionCount,然后在一个简单的select语句中选择它们:

 Declare @computedExceptionCount INT, @manualExceptionCount INT
    Select @computedExceptionCount as computedExceptionCount,@manualExceptionCount as manualExceptionCount

或者您可以尝试这样

SELECT 
  case 
    when ed.computationType LIKE 'manual' then 1 
    else 0 
  end as manualExceptionCount,

  case 
    when ed.computationType LIKE 'computed' then 1
    else 0
  end as computedExceptionCount 
FROM exception AS ex
INNER JOIN
  exceptionDefinition AS ed ON ex.exceptionDefId = ed.exceptionDefId
WHERE ex.customerId='{5B65755C-3B66-434E-AC03-942004E9A27A}'
  AND ex.loanId IS NULL
  AND ex.exceptionState LIKE 'Y'
  AND ex.statusType LIKE 'required'

答案 2 :(得分:0)

这将为你做到:

SELECT 
    sum(case ed.computationType LIKE 'computed' then 1 else 0 end) as computedExceptionCount,
    sum(case ed.computationType LIKE 'manual' then 1 else 0 end) as manualExceptionCount
FROM exception AS ex
JOIN exceptionDefinition AS ed ON ed.exceptionDefId = ex.exceptionDefId
WHERE ex.customerId='{5B65755C-3B66-434E-AC03-942004E9A27A}'
AND ex.loanId IS NULL
AND ex.exceptionState LIKE 'Y'
AND ex.statusType LIKE 'required'
GROUP BY ex.customerId,ed.computationType,ex.exceptionState