我有一张表现金,其结构如下
现金
FileID Cash Date
1 50 03.04.2017
2 100 08.07.2015
3 70 14.09.2018
现在,我想添加一个新列,称为来源,以标识现金来自何处:
FileID Cash Date Source
1 50 03.04.2017 Bank
2 100 08.07.2015 Natural Person
3 70 14.09.2018 Bank
要识别特定来源(例如“自然人”),我正在使用以下选择:
select y.*
from
(select fileID, date, cash, max(date_of_stage) as max_date
from (select c.fileID, c.date, c.cash, s.stage, s.date_of_stage
from cash c
inner join stage s
on c.fileID=s.fileID
and s.date_of_stage < c.date
)
group by fileID, date, cash ) x
inner join
(select c.fileID, c.date, c.cash, s.stage, s.date_of_stage
from cash c
inner join stage s
on c.fileID=s.fileID) y
on x.fileID=y.fileID
and x.date=y.date
and x.cash=y.cash
and x.max_date=y.date_of_stage
where y.fileID is null
现在,我可以使用此选择来识别特定的来源,但是我不知道如何使用上述选择的数据来更新来源列。
我认为可能是这样的:
update cash set Source = 'Natural Person' where
( now I need to identify the c.fileID, c.date, c.cash from the above select somehow)
有什么提示吗?
答案 0 :(得分:0)
查看此内容:
update cash set Source = 'Natural Person' from
cash
inner join
(select y.*
from
(select fileID, date, cash, max(date_of_stage) as max_date
from (select c.fileID, c.date, c.cash, s.stage, s.date_of_stage
from cash c
inner join stage s
on c.fileID=s.fileID
and s.date_of_stage < c.date
)
group by fileID, date, cash ) x
inner join
(select c.fileID, c.date, c.cash, s.stage, s.date_of_stage
from cash c
inner join stage s
on c.fileID=s.fileID) y
on x.fileID=y.fileID
and x.date=y.date
and x.cash=y.cash
and x.max_date=y.date_of_stage
where y.fileID is null) m
on
cash.fileID=m.fileID