[英]Returning 2 count results in statement
我有兩個單獨的查詢來計算我的數據庫中的異常數。 我需要在同一個查詢中返回兩個結果,如何正確地將它們組合在一起?
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
我相信這很簡單……更多的是格式問題
提前謝謝了。
使用案例。
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
為什么不聲明上面的計算異常計數和手動異常計數,然后在一個簡單的選擇語句中選擇兩者:
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'
這將為您做到:
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.