自己紹介... no case文が必要なのは、常に前1件と1件です。ここには2つの方法があります。
このクエリは、ちょうどあなたの要件が
declare @table table (CaseID int, StartDate date, EndDate date, ReviewDate date, [Event] varchar(64))
insert into @table
values
(56,'20170702','20170702','20170702','pre'),
(56,NULL,NULL,NULL,'post'),
(57,'20170704',NULL,NULL,'pre'),
(57,'20170705','20170705','20170705','post'),
(58,NULL,'20170709',NULL,'pre'),
(58,'20170709',NULL,'20170709','post')
select
t1.CaseID
,case when t1.startDate is null and t1.EndDate Is Null and t1.ReviewDate is null then t2.StartDate else t1.StartDate end as StartDate
,case when t1.startDate is null and t1.EndDate Is Null and t1.ReviewDate is null then t2.EndDate else t1.EndDate end as EndDate
,case when t1.startDate is null and t1.EndDate Is Null and t1.ReviewDate is null then t2.ReviewDate else t1.ReviewDate end as ReviewDate
from
@table t1
left join
@table t2 on
t2.CaseID = t1.CaseID
and t2.Event <> t1.Event
where
t1.[Event] = 'post'
を述べたものをこの1つは基本的にNULLの行をマージしないが
select
t1.CaseID
,coalesce(t1.StartDate, t2.StartDate) as StartDate
,coalesce(t1.EndDate,t2.EndDate) as EndDate
,coalesce(t1.ReviewDate,t2.EndDate) as ReviewDate
from
@table t1
left join
@table t2 on
t2.CaseID = t1.CaseID
and t2.Event <> t1.Event
where
t1.[Event] = 'post'
サンプルデータと希望の出力を提供してください –
あなたの試行はどこですか? – Eric
ようこそスタックオーバーフローへ。しばらく時間を取って[ツアー]を受けてください。また、[質問]を読んで質問に答えてください。 –