1
我想更新從當前日期和原始日期之間發生的最早開始日期的日期。我的查詢是:TSQL更新與內聯,分組和
update TableA
set [Stop_Date] =DATA.Min_Start
FROM TableA
JOiN
(select min(TableB.START_DTTM) as Min_Start
from TableB
where
TableB.CancelReason ='Treatment' AND
TableA.Start_Date>=TableB.START_DTTM AND
TableB.START_DTTM '< GETDATE()
) DATA
ON TableA.Ref_No=TableB.REFRL_REFNO
where
[Stop_Date] is null
但是TableA.Start_Date
似乎會導致問題。任何人都可以幫助正確的SQL?
下面是一些測試數據:
DECLARE @TableA TABLE (Ref_No Int, Stop_Date DateTime,Start_Dat DateTime)
DECLARE @TableB TABLE (REFRL_REFNO int, CancelReason varchar(50), START_DTTM DateTime,
Stop_DTTM DateTime,Comments varchar(50))
insert @TableA
select 1, '10 jan 2000', '5 jan 2000'
insert @TableA
select 4, NULL, '9 jan 2000'
insert @TableB
select 1,'Treatment','8 Jan 2000','9 Jan 2000','Shouldn''t be used'
insert @TableB
select 4,'Treatment','1 Jan 2000','2 Jan 2000','Shouldn''t be used'
insert @TableB
select 4,'Treatment','21 Jan 2000','22 Jan 2000','Shouldn''t be used'
insert @TableB
select 4,'Treatment','11 Jan 2000','12 Jan 2000','Should be used'
insert @TableB
select 4,'Other','10 Jan 2000','11 Jan 2000','Shouldn''t be used'
'... AND TableB.START_DTTM)' - 這看起來不像一個完整的表達式。 – mdma 2010-07-15 13:21:14
對不起,'<'標誌沒有通過 – ClimberM 2010-07-15 13:41:28