简体   繁体   English

在语句中返回 2 个计数结果

[英]Returning 2 count results in statement

I have two separate queries that count number of exceptions in my database.我有两个单独的查询来计算我的数据库中的异常数。 I need to return both results in the same query, how do I bring it all together correctly?我需要在同一个查询中返回两个结果,如何正确地将它们组合在一起?

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

I am sure it is something simple.. more of a formatting issue than anything我相信这很简单……更多的是格式问题

Many thanks in advance.提前谢谢了。

Use 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

Why don't you declare computedExceptionCount and manualExceptionCount above, then select both in a simple select statement:为什么不声明上面的计算异常计数和手动异常计数,然后在一个简单的选择语句中选择两者:

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

or you can try like this或者你可以像这样尝试

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'

This will do it for you:这将为您做到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM