I am having some trouble with the syntax when I am trying to use the below query in SQL Server. I wanted to show WHERE
clause based on condition.
This is my code:
DECLARE @isActual = 0
SELECT
SolverRunId, PointId, TimeStampUtc, Value,
UnitOfMeasure, Reliability
FROM
(SELECT
bt.SolverRunId, bt.PointId, bt.TimeStampUtc,
bt.Value, bt.UnitOfMeasure, bt.Reliability
FROM
cte bt
WHERE
bt.TimeStampUtc = bt.TargetTimeUtc
UNION ALL
SELECT
a.SolverRunId, a.PointId, a.TimeStampUtc, a.Value,
a.UnitOfMeasure, a.Reliability
FROM
cte a
WHERE
-- I tried using this case but it is syntactically incorrect
CASE
WHEN @isActual = 0 THEN a.TimeStamUtc > @endDateUtc
ELSE a.TimestampUtc <= @endDateUtc
END
-- instead of this. I wanted to have conditional where based on @isActual value from 0 to 1
a.TimeStampUtc > @endDateUtc
AND a.SolverRunId = @maxRun) x
ORDER BY
SolverRunId, PointId, TimeStampUtc;
I wanted to have the where condition to be evaluated based on @isActual
set to true or false