我有2个表,其中有一个记录的记录如下
CREATE TABLE Student
(
StudentId INT,
StudentName NVARCHAR(200)
)
CREATE TABLE StudentHist
(
StudentHistId INT,
BranchId INT,
StudentId INT,
ExitDate DATE
)
INSERT INTO Student
VALUES (1, 'ABC'), (2, 'PQR')
INSERT INTO StudentHist
VALUES (1, 1, 1, null), (2, 2, 1, '2016-01-01'),
(3, 2, 2, '2017-01-01')
SELECT S.StudentName
FROM Student S
INNER JOIN StudentHist SH ON S.StudentId = SH.StudentId
AND (SH.ExitDate IS NULL OR SH.ExitDate >= getdate())
AND SH.BranchId = 2
当前我有一个查询,该查询给出退出日期为null的记录,我需要的是切换条件,它将检查日期为null并执行当前代码(如果未通过histid获取最新信息并获取日期< / p>
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=1036cec8a82566534665c89f7279ebcb