我正在撤回一些将在某些报告中使用的数据。大多数情况下,这将不返回任何数据,当没有数据返回时,我希望最终用户看到一条消息,当有数据时,我想返回数据元素和不同的消息。我不会仅仅包括CTE部分CASE。它应该足够了。我不确定为什么这不起作用,应该是一个简单的案例。
SELECT Case when Sum(1) IS NULL THEN
'NO DUPES MISSING INFO'
ELSE
CTE.ClaimNum + ' Claim is missing, Please populate'
END AS MissingClaiminfo
FROM CTE
WHERE ClaimCheck > 0
OR CLAIMFormatCk > 0
OR CLAIMIdCheck > 0
OR CLAIM_DupID_Match > 0
Group by CTE.ClaimNum
什么返回没有MissingClaimInfo的记录或消息。
感谢任何帮助。
编辑---以下整个查询:
WITH Nasco_CTE (
CoClaimNum
,ClaimNum
,ClaimCheck
,CLAIMFormatCk
,CLAIMIdCheck
,CLAIMID_DupID_Match
)
AS
(
SELECT
CW.User047 AS CoClaimNum
,CW.ClaimNum AS ClaimNum
,CASE WHEN Left(CW.User047,1) <> 0 THEN 1 ELSE 0 END As ClaimCheck -- Checks for leading zeroes.
,CASE WHEN SubString(CW.User047,LEN(LEFT(CW.User047, CharIndex('- ',CW.User047) )),1) <> '-' THEN 1 ELSE 0 END AS CLAIMFormatCk --- Checks for '-'
,CASE WHEN NASC.NascoClaimId = '' THEN 1 ELSE 0 END AS CLAIMIdCheck
,CASE WHEN NASC.NascoClaimId <> ECW.NascoClaimId THEN 0 ELSE 1 END AS CLAIMID_DupID_Match
FROM Screens.dbo.vClaimsWHSE CW
LEFT JOIN [Screens].dbo.[vClaimsWHSE_RootReason] CWRR
ON CW.ClaimCode = CWRR.Reason
LEFT JOIN [Support].[dbo].[NascoDupID_Crosswalk] NASC
ON NASC.ClaimNum = SUBSTRING(CW.User047,0,CHARINDEX('-',CW.[User047]))
AND NASC.LineSequenceNum = SUBSTRING(CW.User047,CHARINDEX('-',CW. [User047])+1,CHARINDEX('-',CW.[User047]))
LEFT JOIN [Data].[dbo].[ExportCrosswalk] ECW
ON CW.User001 = ECW.ClaimNum
AND CW.User002 = ECW.LineSequenceNum
WHERE CW.user003 IN ('NC','RevRecon')
And NASC.ClaimId IS NOT NULL
)
SELECT Case when COUNT(Nasco_CTE.ClaimNum) ='' THEN
'NO DUPES MISSING INFO'
ELSE
Nasco_CTE.ClaimNum + ' Claim is missing a DupNascoID. Please check check that USER047 is populated correctly: ClaimNum-LineNum EX: 020132952416600-1'
END AS MissingNascoId
FROM Nasco_CTE
WHERE ClaimCheck > 0
OR ClaimCheck > 0
OR CLAIMIdCheck > 0
OR CLAIMID_DupID_Match > 0
Group by Nasco_CTE.ClaimNum
注意当我从ELSE部分删除Nasco_CTE.ClaimNum +时代码有效。并删除该组。
答案 0 :(得分:1)
创建更多CTE:
;WITH Nasco_CTE (/* Columns */)
AS
(/* Definition */
), InitialResults as (
SELECT CTE.ClaimNum + ' Claim is missing, Please populate'
AS MissingClaiminfo, 0 as Ord
FROM CTE
WHERE ClaimCheck > 0
OR CLAIMFormatCk > 0
OR CLAIMIdCheck > 0
OR CLAIM_DupID_Match > 0
Group by CTE.ClaimNum --This might not be needed any more, I think
UNION ALL
SELECT 'NO DUPES MISSING INFO',1
), Ranked as (
SELECT *,RANK() OVER (ORDER BY Ord) as rk
)
SELECT * from Ranked where rk = 1
如果没有Ord
等于1
的行,那么只会生成Ord
0
的行 - 因此您可以获得所需的结果集< em>或单行说NO DUPES MISSING INFO
。
您通常可以使用上述(UNION ALL
和编号结果集)技术:
答案 1 :(得分:0)
Brian,我们可以遵循这样的模式
IF NOT EXISTS(SELECT Statement..)
BEGIN
SELECT 'NO DUPES MISSING INFO'
END
ELSE
BEGIN
SELECT ... FROM CTE WHERE ...
END