SQL Server WHERE-Klausel verwenden Temporäre Spalten
-
19-09-2019 - |
Frage
Ich habe die folgende Abfrage, die eine CASE-Anweisung verwendet. Gibt es trotzdem in die where-Klausel hinzufügen WHERE IsBusinessDayFinal = 0? ohne eine temporäre Tabelle mit?
Vielen Dank!
SELECT
ac.DateTimeValue,
CASE
WHEN pc.IsBusinessDay IS NOT NULL THEN pc.IsBusinessDay
ELSE ac.IsBusinessDay
END AS IsBusinessDayFinal,
ac.FullYear,
ac.MonthValue,
ac.DayOfMonth,
ac.DayOfWeek,
ac.Week
FROM
[dbo].[AdminCalendar] ac LEFT JOIN
[dbo].ProjectCalendar pc ON ac.DateTimeValue = pc.DateTimeValue AND pc.ProjectId = @projectId
WHERE ac.DateTimeValue >= @startDate AND ac.DateTimeValue <= @finishDate;
Lösung
Mit
WHERE (pc.IsBusinessDay IS NULL AND ac.IsBusinessDay = 0)
OR pc.IsBusinessDay = 0
Andere Tipps
WHERE ac.DateTimeValue >= @startDate AND ac.DateTimeValue <= @finishDate
AND ((pc.IsBusinessDay IS NOT NULL AND pc.IsBusinessDay = 0) OR ac.IsBusinessDay = 0)
Sie können mit COALESCE statt von CASE, weil Sie für null sind Überprüfung:
SELECT ac.DateTimeValue,
COALESCE(pc.IsBusinessDay, ac.IsBusinessDay) AS IsBusinessDayFinal,
ac.FullYear,
ac.MonthValue,
ac.DayOfMonth,
ac.DayOfWeek,
ac.Week
FROM [dbo].[AdminCalendar] ac
LEFT JOIN [dbo].ProjectCalendar pc ON ac.DateTimeValue = pc.DateTimeValue
AND pc.ProjectId = @projectId
WHERE ac.DateTimeValue >= @startDate
AND ac.DateTimeValue <= @finishDate
AND COALESCE(pc.IsBusinessDay, ac.IsBusinessDay) = 0
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow