日期:2014-05-18 浏览次数:20512 次
if not object_id('Tempdb..#T') is null drop table #T Go Create table #T([runid] nvarchar(5),[prcsid] int,[use_id] nvarchar(19),[prcs_time] nvarchar(19),[flow_id] nvarchar(19),[end_time] Datetime) Insert #T select N'231 1',45467,N'2011-09-14 15:47:00',N'1',N'2011-11-07 11:06:57',null union all select N'231',2,N'67676',N'2011-09-14 16:35:12',N'1','2011-11-07 11:06:57' union all select N'231',3,N'45454',N'2011-11-07 11:06:57',N'1','2011-11-07 11:06:57' union all select N'235',1,N'45467',N'2011-11-14 15:47:00',N'3',null union all select N'235',2,N'67376',N'2011-11-14 16:35:12',N'3',null union all select N'235',3,N'45554',N'2011-11-07 11:06:57',N'3',null union all select N'235',4,N'45454',N'2011-11-07 11:06:57',N'3',null union all select N'238',1,N'45454',N'2011-11-07 11:06:27',N'6',null union all select N'239',1,N'35467',N'2011-10-14 15:47:00',N'6',null union all select N'239',2,N'67376',N'2011-11-14 16:35:12',N'6',null union all select N'239',3,N'46554',N'2011-11-07 11:06:57',N'6',null union all select N'239',4,N'46454',N'2011-11-07 11:06:57',N'6',null union all select N'239',5,N'25454',N'2011-11-07 11:06:57',N'6',null union all select N'236',1,N'35467',N'2011-11-17 15:47:00',N'7','2011-11-22 11:06:57' union all select N'236',2,N'67376',N'2011-11-19 16:35:12',N'7','2011-11-22 11:06:57' union all select N'236',3,N'46d54',N'2011-11-20 11:06:57',N'7','2011-11-22 11:06:57' union all select N'236',4,N'46c54',N'2011-11-21 11:06:57',N'7','2011-11-22 11:06:57' union all select N'236',5,N'25454',N'2011-11-22 11:06:57',N'7','2011-11-22 11:06:57' union all select N'266',1,N'25654',N'2011-11-22 16:06:57',N'8',null Go select a.[runid],a.[prcsid],a.use_id,a.prcs_time, case when [flow_id] in(1,3,6) then N'上海' when [flow_id] in(7,8) then N'北京' end as 区域 , isnull(convert(nvarchar(19),end_time,120),N'(未结束)')+case when end_time is not null then N'(已结束)' else '' end as end_time, CASE WHEN a.prcsid=b.[MaxPrcsid] AND end_time IS NOT null THEN N'结束' ELSE '' END AS 结束位置 from #T as a inner join (Select [runid],MAX([prcsid]) AS [MaxPrcsid] from #T AS a WHERE a.[prcs_time] >= '2011-11-01' AND a.[prcs_time]<'2011-12-01' and EXISTS(SELECT 1 FROM #T WHERE runid=a.runid having a.[prcsid]=1 or a.prcsid=max([prcsid])) group by [r