我将几个DischargeType值汇总到一个名为' Appeals'的类别中。这工作正常但是聚合字段MIN(DateReceived)导致结果为每个Appeals类型产生1个DateReceived值,当我只想要一个整个Appeals类别。我想我可以在GROUP BY子句中添加相同的CASE语句,但我不知道如何使用ROLL UP功能执行此操作。最后一行中的NULL值是总计行。感谢
<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>
答案 0 :(得分:1)
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
答案 1 :(得分:1)
您需要value
构造:
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;