如果我说得对,您想从 Table2 中获取日期时间字段为空或将来有值的行。你说你希望未来的日期优先于 NULL 值,所以我知道你想要一个最大值。让我们设置一些示例数据:
CREATE TABLE Table1 (id_table1 int not null primary key)
INSERT INTO Table1 (id_table1)
VALUES (1)
INSERT INTO Table1 (id_table1)
VALUES (2)
INSERT INTO Table1 (id_table1)
VALUES (3)
INSERT INTO Table1 (id_table1)
VALUES (4)
INSERT INTO Table1 (id_table1)
VALUES (5)
CREATE TABLE Table2 (id_table2 int not null primary key, id_table1 int, date_value datetime)
INSERT INTO Table2 (id_table2,id_table1,date_value)
VALUES (1,1,'2001-01-01 00:00:00')
INSERT INTO Table2 (id_table2,id_table1,date_value)
VALUES (2,1,NULL)
INSERT INTO Table2 (id_table2,id_table1,date_value)
VALUES (3,1,'2015-01-01 00:00:00')
INSERT INTO Table2 (id_table2,id_table1,date_value)
VALUES (4,2, '2002-01-01 00:00:00')
INSERT INTO Table2 (id_table2,id_table1,date_value)
VALUES (5,3, '2003-01-01 00:00:00')
INSERT INTO Table2 (id_table2,id_table1,date_value)
VALUES (6,3, '2018-01-01 00:00:00')
INSERT INTO Table2 (id_table2,id_table1,date_value)
VALUES (7,4, '2004-01-01 00:00:00')
INSERT INTO Table2 (id_table2,id_table1,date_value)
VALUES (8,4,NULL)
现在 Table2 中每个 id_table1 都有 0..n 行。
首先,您可以从 Table2 中获取符合条件 NULL 或将来的所有行:
declare @datThreshold datetime
set @datThreshold=getdate()
SELECT *
FROM Table2 t2
WHERE t2.date_value is null OR t2.date_value>@datThreshold
正如我所说,我了解您希望 NULL 最低的剩余日期的最大值。您可以通过将 NULL 值设置为默认值来实现这一点,默认值可以是 @datThreshold,因为由于 WHERE 条件,这将是最低值。
declare @datThreshold datetime
set @datThreshold=getdate()
SELECT id_table1, max(isnull(t2.date_value,@datThreshold)) max_date
FROM Table2 t2
WHERE t2.date_value is null OR t2.date_value>@datThreshold
GROUP BY t2.id_table1
现在每个 id_table1 都有一行,并且必须将其加入 Table1:
declare @datThreshold datetime
set @datThreshold=getdate()
SELECT t1.id_table1,max.id_table1,max.max_date
FROM Table1 t1
JOIN (SELECT id_table1, max(isnull(t2.date_value,@datThreshold)) max_date
FROM Table2 t2
WHERE t2.date_value is null OR t2.date_value>@datThreshold
GROUP BY t2.id_table1) max ON t1.id_table1=max.id_table1
您也可以再次将默认日期替换为 NULL:
declare @datThreshold datetime
set @datThreshold=getdate()
SELECT t1.id_table1,max.id_table1,case max.max_date when @datThreshold then NULL else max.max_date end date_value
FROM Table1 t1
JOIN (SELECT id_table1, max(isnull(t2.date_value,@datThreshold)) max_date
FROM Table2 t2
WHERE t2.date_value is null OR t2.date_value>@datThreshold
GROUP BY t2.id_table1) max ON t1.id_table1=max.id_table1