34,590
社区成员
发帖
与我相关
我的任务
分享
select '600001' as companycode,'20110715' as PriceDate,15.5 as PriceEnd
into #DayPrice
union all
select '600001' as companycode,'20110714' as PriceDate,15.4 as PriceEnd
union all
select '600001' as companycode,'20110713' as PriceDate,15.3 as PriceEnd
union all
select '600001' as companycode,'20110712' as PriceDate,15.2 as PriceEnd
union all
select '600001' as companycode,'20110711' as PriceDate,15.1 as PriceEnd
union all
select '600002' as companycode,'20110715' as PriceDate,25.5 as PriceEnd
union all
select '600002' as companycode,'20110714' as PriceDate,25.4 as PriceEnd
union all
select '600002' as companycode,'20110713' as PriceDate,25.3 as PriceEnd
union all
select '600002' as companycode,'20110712' as PriceDate,25.2 as PriceEnd
union all
select '600002' as companycode,'20110711' as PriceDate,25.1 as PriceEnd
select a.companycode,count(a.PriceDate) as DayCount
,(select sum(PriceEnd) from
(select top 3 PriceEnd from #DayPrice as aa where aa.companycode=a.companycode order by PriceDate desc) t
) as Price3
from #DayPrice as a
group by a.companycode
go
drop table #DayPrice
/******************************************(2000是这样的结果)
companycode DayCount Price3
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
--------------------------
--我希望的应该是返回(2008返回这样的结果)
companycode DayCount Price3
600001 5 46.2
600002 5 76.2
select a.companycode,(select sum(PriceEnd) from #DayPrice aa where companycode=a.companycode
and PriceDate in (select top 3 PriceDate from #DayPrice bb
where companycode=a.companycode order by PriceDate desc))
from #DayPrice as a
group by a.companycode
--2000
select '600001' as companycode,'20110715' as PriceDate,15.5 as PriceEnd
into #DayPrice
union all
select '600001' as companycode,'20110714' as PriceDate,15.4 as PriceEnd
union all
select '600001' as companycode,'20110713' as PriceDate,15.3 as PriceEnd
union all
select '600001' as companycode,'20110712' as PriceDate,15.2 as PriceEnd
union all
select '600001' as companycode,'20110711' as PriceDate,15.1 as PriceEnd
union all
select '600002' as companycode,'20110715' as PriceDate,25.5 as PriceEnd
union all
select '600002' as companycode,'20110714' as PriceDate,25.4 as PriceEnd
union all
select '600002' as companycode,'20110713' as PriceDate,25.3 as PriceEnd
union all
select '600002' as companycode,'20110712' as PriceDate,25.2 as PriceEnd
union all
select '600002' as companycode,'20110711' as PriceDate,25.1 as PriceEnd
go
select a.companycode,count(a.PriceDate) as DayCount
,(select sum(PriceEnd) from
(select top 3 PriceEnd from #DayPrice as aa where aa.companycode=a.companycode order by PriceDate desc) t
) as Price3
from #DayPrice as a
group by a.companycode
drop table #DayPrice
/***********
companycode DayCount Price3
----------- ----------- ----------------------------------------
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600001 5 46.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
600002 5 76.2
select a.companycode,
(select count(1) from #DayPrice b where a.companycode=b.companycode) as DayCount,
(select sum(PriceEnd) from
(select top 3 PriceEnd from #DayPrice b where a.companycode=b.companycode order by PriceDate desc) c
) Price3
from (select distinct companycode from #DayPrice) a
select '600001' as companycode,'20110715' as PriceDate,15.5 as PriceEnd
into #DayPrice
union all
select '600001' as companycode,'20110714' as PriceDate,15.4 as PriceEnd
union all
select '600001' as companycode,'20110713' as PriceDate,15.3 as PriceEnd
union all
select '600001' as companycode,'20110712' as PriceDate,15.2 as PriceEnd
union all
select '600001' as companycode,'20110711' as PriceDate,15.1 as PriceEnd
union all
select '600002' as companycode,'20110715' as PriceDate,25.5 as PriceEnd
union all
select '600002' as companycode,'20110714' as PriceDate,25.4 as PriceEnd
union all
select '600002' as companycode,'20110713' as PriceDate,25.3 as PriceEnd
union all
select '600002' as companycode,'20110712' as PriceDate,25.2 as PriceEnd
union all
select '600002' as companycode,'20110711' as PriceDate,25.1 as PriceEnd
go
select a.companycode,count(a.PriceDate) as DayCount
,(select sum(PriceEnd) from
(select top 3 PriceEnd from #DayPrice as aa where aa.companycode=a.companycode order by PriceDate desc) t
) as Price3
from #DayPrice as a
group by a.companycode
go
drop table #DayPrice
/*********
companycode DayCount Price3
----------- ----------- ---------------------------------------
600001 5 46.2
600002 5 76.2
(2 行受影响)