问题
Below are some example results that I am expecting from my script: Example 1: Customer 12 was provided only service 'NEI2'. then result should be
AcctNum PStatus IStatus
12 1 5
Example 2: If customer 21 had service 'PN20', 'PN4', and 'FL1' then the result should be
AcctNum PStatus IStatus
21 3 2
21 4 5
Response will always be 'Y'. You can modify the script if you need to. Thank you.
Below is the script:
SELECT distinct A.AcctNum,
CASE
WHEN O.Order = 'NEI2' THEN '1'
WHEN AV.Query IN ('PNE1','PNE2') AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'PN20' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query = 'PN4' AND AV.Response = 'Y' THEN '4'
ELSE '5'
END AS [PStatus],
CASE
WHEN O.Order IN ('DO2','FL25','VACHP') THEN '1'
WHEN AV.Query = 'FL1' AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'REF' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query IN ('FL2','FL6','NEU.G','HE.B') AND AV.Response = 'Y' THEN '4'
WHEN AV.Query = 'NOA' AND AV.Response = 'Y' THEN '6'
ELSE '5'
END AS [IStatus]
FROM AData AS AD
INNER JOIN AVisit AS AV
ON AD.Visit = AV.Visit
AND AV.QueryID IN ('PNE1','PNE2','PN20','PN4','FL1','REF','FL2','FL6','NEU.G','HE.B','NOA')
LEFT JOIN Order AS O
ON AD.Visit = O.Visit
AND O.Order IN ('NEI2','DO2','FL25','VACHP');
回答1:
Do you want the group by
by the statuses?
SELECT AcctNum,
(CASE WHEN O.Order = 'NEI2' THEN '1'
WHEN AV.Query IN ('PNE1','PNE2') AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'PN20' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query = 'PN4' AND AV.Response = 'Y' THEN '4'
ELSE NULL
END) AS [Pstatus],
(CASE WHEN O.Order IN ('DO2','FL25','VACHP') THEN '1'
WHEN AV.Query = 'FL1' AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'REF' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query IN ('FL2','FL6','NEU.G','HE.B') AND AV.Response = 'Y' THEN '4'
WHEN AV.Query = 'NOA' AND AV.Response = 'Y' THEN '6'
ELSE NULL
END) AS [IStatus]
FROM AData AS AD INNER JOIN
AVisit AS AV
ON AD.Visit = AV.Visit
WHERE AV.QueryID IN ('PNE1', 'PNE2', 'PN20', 'PN4', 'FL1', 'REF', 'FL2','FL6', 'NEU.G','HE.B','NOA') LEFT JOIN
Order AS O
ON AD.Visit = O.Visit AND O.Order IN ('NEI2','DO2','FL25','VACHP');
GROUP BY AcctNum,
(CASE WHEN O.Order = 'NEI2' THEN '1'
WHEN AV.Query IN ('PNE1','PNE2') AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'PN20' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query = 'PN4' AND AV.Response = 'Y' THEN '4'
ELSE NULL
END) AS [Pstatus],
(CASE WHEN O.Order IN ('DO2','FL25','VACHP') THEN '1'
WHEN AV.Query = 'FL1' AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'REF' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query IN ('FL2','FL6','NEU.G','HE.B') AND AV.Response = 'Y' THEN '4'
WHEN AV.Query = 'NOA' AND AV.Response = 'Y' THEN '6'
ELSE NULL
END) AS [IStatus];
回答2:
I have modified your query to group by
AcctNum
, and values of [Pstatus]
and [IStatus]
.
Do not believe it is possible to achieve what you are suggesting in example 2 - i.e producing a SQL grouping/merging rule to consolidate row values (3,5)(4,5)(5,2) into (3,2)(4,5):
Example 2: If customer 21 had service 'PN20', 'PN4', and 'FL1' then the result should be
AcctNum PStatus IStatus
21 3 2
21 4 5
My suggestion is to group your data to AcctNum
, and values of [Pstatus]
and [IStatus]
because anything "more" would require complexity beyond it's worth:
SELECT
AcctNum,
(CASE
WHEN O.Order = 'NEI2' THEN '1'
WHEN AV.Query IN ('PNE1','PNE2') AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'PN20' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query = 'PN4' AND AV.Response = 'Y' THEN '4'
ELSE '5'
END
) AS [Pstatus],
(CASE
WHEN O.Order IN ('DO2','FL25','VACHP') THEN '1'
WHEN AV.Query = 'FL1' AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'REF' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query IN ('FL2','FL6','NEU.G','HE.B') AND AV.Response = 'Y' THEN '4'
WHEN AV.Query = 'NOA' AND AV.Response = 'Y' THEN '6'
ELSE '5'
END
) AS [IStatus]
FROM AData AS AD
INNER JOIN AVisit AS AV ON AD.Visit = AV.Visit AND
AV.QueryID IN ('PNE1','PNE2','PN20','PN4','FL1','REF','FL2','FL6','NEU.G','HE.B','NOA')
LEFT JOIN Order AS O ON AD.Visit = O.Visit AND
O.Order IN ('NEI2','DO2','FL25','VACHP');
GROUP BY AcctNum,
(CASE
WHEN O.Order = 'NEI2' THEN '1'
WHEN AV.Query IN ('PNE1','PNE2') AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'PN20' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query = 'PN4' AND AV.Response = 'Y' THEN '4'
ELSE '5'
END),
(CASE
WHEN O.Order IN ('DO2','FL25','VACHP') THEN '1'
WHEN AV.Query = 'FL1' AND AV.Response = 'Y' THEN '2'
WHEN AV.Query = 'REF' AND AV.Response = 'Y' THEN '3'
WHEN AV.Query IN ('FL2','FL6','NEU.G','HE.B') AND AV.Response = 'Y' THEN '4'
WHEN AV.Query = 'NOA' AND AV.Response = 'Y' THEN '6'
ELSE '5'
END)
来源:https://stackoverflow.com/questions/28311963/case-statement-logic-not-fully-functional