22,300
社区成员




--测试数据
create table #TableFin(
股票代码 varchar(15),
公告日 varchar(15),
净利润 varchar(15),
净资产 varchar(15)
)
insert into #TableFin
select '600000.SH', '20150213','0.23亿元','12亿元' union all
select'600000.SH', '20150413','0.93亿元','13亿元'union all
select'000001.SZ', '20150423','0.17亿元','5亿元'union all
select'000001.SZ', '20150923','0.88亿元','6亿元'
--查询
select a.* from #TableFin a,
(
SELECT 股票代码,MAX(公告日)as '公告日'
FROM #TableFin
Where 公告日<'20150601'
Group By 股票代码
)b
where a.股票代码=b.股票代码 and a.公告日=b.公告日
--结果
/*
股票代码 公告日 净利润 净资产
--------------- --------------- --------------- ---------------
600000.SH 20150413 0.93亿元 13亿元
000001.SZ 20150423 0.17亿元 5亿元
(2 行受影响)
*/
select * from
(
select row_number()over(partition by 股票代码 order by 公告日 desc) * from TableFin where 公告日<‘20150601’
) as t where t.seqno=1
select * from
(
select row_number()over(partition by 股票代码 order by 公告日 desc) as seqNo, * from TableFin where 公告日<‘20150601’
) as t where t.seqno=1
咋自己写的都不允许编辑