January 24, 2018 at 9:14 am
Hi,
I can't get rid of the red squiggle under the IF statement.
The code is part of a Create Stored Procedure construct.
Any thoughts?
The code worked fine until I added the IF clause to test for the date.
Cheers,
Julian
WITH myCTE as
( SELECT COUNT(*) OVER (PARTITION BY DATEPART(ISO_WEEK, H.BackupDatum), H.zzpclient_id ) AS ROWSperWEEKperBEW,
CONVERT(DATE,H.BackupDatum) AS BACKUPDATUM,
H.zzpclient_id,
ISNULL(H.[Spiegel+TOT],0)-ISNULL(H.ZPTHrsMeerzorg,0) AS SpiegelUren
FROM Bewoners_HISTORY H
WHERE CONVERT(DATE,H.BackupDatum) BETWEEN @DatumBegin AND @DatumEnd
AND H.ZPTIDHuis = @IDHuis
AND H.ZPTIDAfd = @IDAfd
AND LEFT(H.zzpafdeling_oms,3) <> 'GRZ'
AND H.AfdMeetellen <> 'NEE'
AND (H.ZPTCALC <> 'N' OR H.ZPTCALC IS NOT NULL)
)
, myGoodCTE as
(
SELECT C.BackupDatum,
C.zzpclient_id,
-- Sum all the hours of the Bewoners per BackupDatum, after dividing by RowsPerWeekPerBew
SUM(C.[SpiegelUren])/ROWSperWEEKperBEW AS [UrenSpiegel+]
FROM myCTE C
GROUP BY C.BackupDatum, C.zzpclient_id, C.ROWSperWEEKperBEW
)
--> IF dbo.ISOyear(@DatumBegin) >=2018
BEGIN
(SELECT M.BACKUPDATUM, SUM(M.[UrenSpiegel+])
FROM myGoodCTE M
GROUP BY M.BACKUPDATUM);
END
ELSE
BEGIN
(SELECT DatumData, SUM(SpiegelNetto) + SUM(HV2NormHrs) as SpiegelPlus
FROM LocatieFaktor_Historie
WHERE IDHuis = @IDHuis And
IDAfd = @IDAfd AND
DatumData Between @DatumBegin And @DatumEnd
GROUP BY DatumData);
END
January 24, 2018 at 9:20 am
A CTE must be followed immediately by a query. Yours is not, it's followed by an IF statement.
The IF needs to go outside of the CTE.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
January 24, 2018 at 9:57 am
Okay, thank you.
J.
January 24, 2018 at 9:57 am
GilaMonster - Wednesday, January 24, 2018 9:20 AMA CTE must be followed immediately by a query. Yours is not, it's followed by an IF statement.
The IF needs to go outside of the CTE.
This makes it sound like the CTE is somehow separate from the query. In fact, the CTE is part of the query in the same way that a WHERE clause or a GROUP BY clause are part of the query. A SELECT statement needs to be atomic, and inserting an IF statement inside of a SELECT statement would potentially invalidate that property.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
January 24, 2018 at 1:09 pm
flip it around, do IF dbo.ISOyear(@DatumBegin) >=2018 BEGIN <select from the CTE as written with your SELECT right after> END ELSE BEGIN <your other query> END
-------------------------------------------------------------------------------------------------------------------------------------
Please follow Best Practices For Posting On Forums to receive quicker and higher quality responses
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply