-3 sql sql-server max
表格1:
client report_date date_of_analysis
554 30.12.2013 15.01.2014
554 30.12.2013 25.01.2014
554 30.12.2012 15.01.2013
554 30.12.2012 28.01.2013
554 30.12.2011 15.01.2012
556 30.12.2013 15.01.2014
556 30.12.2012 15.01.2013
556 30.12.2011 15.01.2012
556 30.12.2011 05.01.2012
Run Code Online (Sandbox Code Playgroud)
当有两份报告并且分析日期较旧时,我想用前面的x更新客户编号.
在这种情况下,它应该用分析日期15.01.2014更新一个客户端554.报告日期为2013年12月30日.现在应该是'x554'.
我试过了:
update table1
set client= 'x'+client
where date_of_analysis<max(date_of_analysis)
Run Code Online (Sandbox Code Playgroud)
但这不起作用
结果将是:
client report_date date_of_analysis
X554 30.12.2013 15.01.2014
554 30.12.2013 25.01.2014
X554 30.12.2012 15.01.2013
554 30.12.2012 28.01.2013
554 30.12.2011 15.01.2012
556 30.12.2013 15.01.2014
556 30.12.2012 15.01.2013
556 30.12.2011 15.01.2012
X556 30.12.2011 05.01.2012
Run Code Online (Sandbox Code Playgroud)
假设date_of_analysis是日期时间或日期
;with x as
(
select client, date_of_analysis,
row_number() over (partition by client, report_date
order by date_of_analysis desc) rn
from <table>
where client not like 'x%'
)
update x
set client = 'x' + client
where rn > 1
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
85 次 |
最近记录: |