Reputation: 1663
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.
Upvotes: 2
Views: 74
Reputation: 6405
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
Upvotes: 0
Reputation: 104
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'
Upvotes: 0
Reputation: 6076
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
Upvotes: 2