I'm lumping together several DischargeType values into one category named 'Appeals'. This works fine but the aggregate field MIN(DateReceived) is causing the results to produce 1 DateReceived value for every Appeals type when I just want one for the whole category of Appeals. I thought I could just add the same CASE statement in the GROUP BY clause but I don't know how to do this with the ROLL UP function. The NULL value in the last row is the totals row. Thanks
<PRE>
CREATE PROCEDURE [dbo].[p_ReportMonthlySAEProcessing]
@StartDate smalldatetime = NULL,
@EndDate smalldatetime = NULL
AS
BEGIN
SELECT
CASE
WHEN DischargeType = 'dqs' THEN 'Disqualifying Status'
WHEN DischargeType = 'fraud' THEN 'Fraud'
WHEN DischargeType = 'id theft' THEN 'ID Theft'
WHEN DischargeType = 'unenforceable' THEN 'Unenforceable'
WHEN DischargeType = 'unp' THEN 'Unpaid Refund'
WHEN DischargeType = 'uns' THEN 'Unauthorized Signature/Payment'
WHEN DischargeType IN ('atb appeal', 'cls appeal','dqs appeal','id
theft appeal','unp appeal','uns appeal') THEN 'Appeals'
END AS DischargeType
,SUM(CASE WHEN (DateReceived > @StartDate AND DateReceived < DATEADD(dd,
1, @EndDate)) THEN 1 ELSE 0 END) AS Claims_Received
,SUM(CASE WHEN (DateCompleted > @StartDate AND DateCompleted <
DATEADD(dd, 1, @EndDate)) THEN 1 ELSE 0 END) AS Claims_Completed
,SUM(CASE WHEN DateCompleted IS NULL THEN 1 ELSE 0 END) AS Claims_Pending
,MIN(DateReceived) AS [Oldest_Claim]
FROM
Claims
GROUP BY
DischargeType WITH ROLLUP
ORDER BY
CASE WHEN DischargeType IS NULL THEN 1 ELSE 0 END, DischargeType</pre>
I would use a CTE https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql?view=sql-server-2017
WITH claimsGrp AS (
SELECT
CASE
WHEN DischargeType = 'dqs' THEN 'Disqualifying Status'
WHEN DischargeType = 'fraud' THEN 'Fraud'
WHEN DischargeType = 'id theft' THEN 'ID Theft'
WHEN DischargeType = 'unenforceable' THEN 'Unenforceable'
WHEN DischargeType = 'unp' THEN 'Unpaid Refund'
WHEN DischargeType = 'uns' THEN 'Unauthorized Signature/Payment'
WHEN DischargeType IN ('atb appeal', 'cls appeal','dqs appeal','id
theft appeal','unp appeal','uns appeal') THEN 'Appeals'
END AS DischargeType
,
CASE WHEN (DateReceived > @StartDate AND DateReceived < DATEADD(dd,
1, @EndDate)) THEN 1 ELSE 0 END AS Claims_Received
,CASE WHEN (DateCompleted > @StartDate AND DateCompleted <
DATEADD(dd, 1, @EndDate)) THEN 1 ELSE 0 END AS Claims_Completed
,CASE WHEN DateCompleted IS NULL THEN 1 ELSE 0 END AS Claims_Pending
,DateReceived AS [Oldest_Claim]
FROM
Claims)
SELECT claimsGrp.DischargeType,
SUM(claimsGrp.Claims_Received),
SUM(claimsGrp.Claims_Completed),
SUM(claimsGrp.Claims_Pending)
min([Oldest_Claim])
FROM claimsGrp
GROUP BY
DischargeType WITH ROLLUP
ORDER BY
CASE WHEN DischargeType IS NULL THEN 1 ELSE 0 END, DischargeType
You need value
construct :
SELECT cc.DischargeType,
. . .
FROM Claims c CROSS APPLY (
VALUES (CASE WHEN DischargeType = 'dqs' THEN 'Disqualifying Status'
WHEN DischargeType = 'fraud' THEN 'Fraud'
WHEN DischargeType = 'id theft' THEN 'ID Theft'
WHEN DischargeType = 'unenforceable' THEN 'Unenforceable'
WHEN DischargeType = 'unp' THEN 'Unpaid Refund'
WHEN DischargeType = 'uns' THEN 'Unauthorized Signature/Payment'
WHEN DischargeType IN ('atb appeal', 'cls appeal','dqs appeal','id
theft appeal','unp appeal','uns appeal') THEN 'Appeals'
END)
) cc (DischargeType)
GROUP BY cc.DischargeType WITH ROLLUP;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.