日期:2014-05-18 浏览次数:20624 次
declare @c_id int SEt @c_id =19 select top 10000 * into #temp from [CarVehicleData_tbl] where c_id=@c_id order by [CSN_UpdateTime] desc select max([CSN_ID]) as [CSN_ID], c_id, [CSN_Part],[CSN_Type], max([CSN_UpdateTime]) as [CSN_UpdateTime] into #temp2 from #temp group by c_id ,[CSN_Part],[CSN_Type] select *,(select top 1 [CSN_DataOriginal] from [CarVehicleData_tbl] a where b.csn_updateTime=a.csn_updateTime order by csn_updateTime desc ) as [CSN_DataOriginal] into #temp3 from #temp2 b select c_id,[CSN_Part],data1 =max( case csn_type when 0 then [CSN_DataOriginal] end ),data2=max( case csn_type when 1 then [CSN_DataOriginal] end ) into #temp4 from #temp3 group by c_id,[CSN_Part] select *,null as a ,null as b from #temp3 drop table #temp drop table #temp2 drop table #temp3 drop table #temp4
declare @c_id int SEt @c_id =19 select top 10000 * into #temp from [CarVehicleData_tbl] where c_id=@c_id order by [CSN_UpdateTime] DESC CREATE INDEX IX_#temp_ALL ON #temp(c_id,[CSN_Part],csn_type,csn_updateTime,CSN_DataOriginal) INCLUDE ([CSN_DataOriginal]) select a.c_id, a.[CSN_Part], data1=MAX(CASE WHEN a.[CSN_Type]=0 THEN b.csn_updateTime IS NOT NULL THEN a.CSN_DataOriginal end), data2=MAX(CASE WHEN a.[CSN_Type]=1 THEN b.csn_updateTime IS NOT NULL THEN a.CSN_DataOriginal end