丑代码展示

所有信息来源一个表。这个表存了各个fund在每个年报中的信息,如performance fee、NetExpenseRatio、OngoingCharge等等许多。对于提到的三个字段,每期年报不一定有值,现要求取有值的最新的那期,应该如何写代码?

--For table NetExpenseRatio
select Id,AnnualReportDate,NetExpenseRatio into #Temp1 from RawData.dbo.ViewAnnualReport
where NetExpenseRatio is not NULL
select Table1.Id, Table1.AnnualReportDate into #Temp001 from (select Id,AnnualReportDate,NetExpenseRatio,ROW_NUMBER() over (partition by Id order by AnnualReportDate Desc) RowNumber
from #Temp1) Table1
where Table1.RowNumber = 1

--For table OngoingCharge
select Id,AnnualReportDate,OngoingCharge into #Temp2 from RawData.dbo.ViewAnnualReport
where OngoingCharge is not NULL
select Table2.Id, Table2.AnnualReportDate into #Temp002 from (select Id,AnnualReportDate,OngoingCharge,ROW_NUMBER() over (partition by Id order by AnnualReportDate Desc) RowNumber
from #Temp2) Table2
where Table2.RowNumber = 1

--For table PerformanceFee
select Id,AnnualReportDate,PerformanceFee into #Temp3 from RawData.dbo.ViewAnnualReport
where PerformanceFee is not NULL
select Table3.Id, Table3.AnnualReportDate into #Temp003 from (select Id,AnnualReportDate,PerformanceFee,ROW_NUMBER() over (partition by Id order by AnnualReportDate Desc) RowNumber
from #Temp3) Table3
where Table3.RowNumber = 1
-------------------------------------------------------------------------------------------------------------------------------------------

--OverLap Table All
select * into #OverLap from #Temp001 Intersect select * from #Temp002 Intersect select * from #Temp003

--OverLap Temp Table 1&2
(Select * into #Temp12 From #Temp001 Intersect select * from #Temp002) Except (Select * from #OverLap)

--OverLap Temp Table 1&3
(Select * into #Temp13 From #Temp001 Intersect select * from #Temp003) Except (Select * from #OverLap)

--OverLap Temp Table 2&3
(Select * into #Temp23 From #Temp002 Intersect select * from #Temp003) Except (Select * from #OverLap)

--Exception #ExTemp1

Select * into #ExTemp1 from #Temp001 Except (Select * from #Temp002 union all (select * from #Temp003))

--Exception #ExTemp2

Select * into #ExTemp2 from #Temp002 Except (Select * from #Temp001 union all (select * from #Temp003))

--Exception #ExTemp3

Select * into #ExTemp3 from #Temp003 Except (Select * from #Temp001 union all (select * from #Temp002))

-------------------------------------------------------------------------------------------------------------------------------------------
--FindOutData
select Main.Id,Main.AnnualReportDate,Main.NetExpenseRatio,Main.OngoingCharge,Main.PerformanceFee from RawData.dbo.ViewAnnualReport Main right join #OverLap a on Main.Id=a.Id and Main.AnnualReportDate=a.AnnualReportDate
union (select Main.Id,Main.AnnualReportDate,Main.NetExpenseRatio,Main.OngoingCharge,Main.PerformanceFee from RawData.dbo.ViewAnnualReport Main right join #Temp12 b on Main.Id=b.Id and Main.AnnualReportDate=b.AnnualReportDate)
union (select Main.Id,Main.AnnualReportDate,Main.NetExpenseRatio,Main.OngoingCharge,Main.PerformanceFee from RawData.dbo.ViewAnnualReport Main right join #Temp13 c on Main.Id=c.Id and Main.AnnualReportDate=c.AnnualReportDate)
union (select Main.Id,Main.AnnualReportDate,Main.NetExpenseRatio,Main.OngoingCharge,Main.PerformanceFee from RawData.dbo.ViewAnnualReport Main right join #Temp23 d on Main.Id=d.Id and Main.AnnualReportDate=d.AnnualReportDate)
union (select Main.Id,Main.AnnualReportDate,Main.NetExpenseRatio,Main.OngoingCharge,Main.PerformanceFee from RawData.dbo.ViewAnnualReport Main right join #ExTemp1 e on Main.Id=e.Id and Main.AnnualReportDate=e.AnnualReportDate)
union (select Main.Id,Main.AnnualReportDate,Main.NetExpenseRatio,Main.OngoingCharge,Main.PerformanceFee from RawData.dbo.ViewAnnualReport Main right join #ExTemp2 f on Main.Id=f.Id and Main.AnnualReportDate=f.AnnualReportDate)
union (select Main.Id,Main.AnnualReportDate,Main.NetExpenseRatio,Main.OngoingCharge,Main.PerformanceFee from RawData.dbo.ViewAnnualReport Main right join #ExTemp3 g on Main.Id=g.Id and Main.AnnualReportDate=g.AnnualReportDate)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

取啥都被占用

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值