Get the leader of each group and sort them in descending order:
with grp(Name,ImpFile,TimeGroup,ImpTime) as ( select cast(null as varchar(5)), ImpFile, max(ImpTime) as TimeGroup, max(ImpTime) as ImpTime from people group by ImpFile ) select * from grp order by TimeGroup desc;
Output:
NAME IMPFILE TIMEGROUP IMPTIME (null) Import12 2012-05-16 09:55:37.3870000 2012-05-16 09:55:37.3870000 (null) Imp01 2012-05-16 09:54:02.4780000 2012-05-16 09:54:02.4780000
Then attach followers to the leader and get the leader time (TimeGroup):
with grp(Name,ImpFile,TimeGroup,ImpTime) as ( select cast(null as varchar(5)), ImpFile, max(ImpTime) as TimeGroup, max(ImpTime) as ImpTime from people group by ImpFile union all select p.Name, p.ImpFile, ldr.TimeGroup, p.ImpTime from people p inner join grp ldr
Output:
NAME IMPFILE IMPTIME (null) Import12 2012-05-16 09:55:37.3870000 Bart Import12 2012-05-16 09:55:37.3870000 John Import12 2012-05-16 09:55:37.3840000 Sasha Import12 2012-05-16 09:55:37.3850000 (null) Imp01 2012-05-16 09:54:02.4780000 Ann Imp01 2012-05-16 09:54:02.4780000 Mark Imp01 2012-05-16 09:54:02.4770000 Sam Imp01 2012-05-16 09:54:02.4770000
The logic of the request is that we bind followers (those who have a name) to their leadership time (TimeGroup) based on ImpFile. The leader and his followers have the same time groups, so when we sort them by time, they will stick to each other; then after that we sort by name
Live test: http://www.sqlfiddle.com/#!3/c7859/21
If we want the group leader to appear after his followers, just put the case when on ORDER BY:
with grp(Name,ImpFile,TimeGroup,ImpTime) as ( select cast(null as varchar(5)), ImpFile, max(ImpTime) as TimeGroup, max(ImpTime) as ImpTime from people group by ImpFile union all select p.Name, p.ImpFile, ldr.TimeGroup, p.ImpTime from people p inner join grp ldr -- leader on ldr.name is null and ldr.ImpFile = p.ImpFile ) select Name, ImpFile, ImpTime from grp order by TimeGroup desc, case when Name is null then 2 -- leader last else 1 -- followers first end, Name
Output:
NAME IMPFILE IMPTIME Bart Import12 2012-05-16 09:55:37.3870000 John Import12 2012-05-16 09:55:37.3840000 Sasha Import12 2012-05-16 09:55:37.3850000 (null) Import12 2012-05-16 09:55:37.3870000 Ann Imp01 2012-05-16 09:54:02.4780000 Mark Imp01 2012-05-16 09:54:02.4770000 Sam Imp01 2012-05-16 09:54:02.4770000 (null) Imp01 2012-05-16 09:54:02.4780000
Live test: http://www.sqlfiddle.com/#!3/c7859/23
How it works:
with grp(Name,ImpFile,TimeGroup,ImpTime) as ( select cast(null as varchar(5)), ImpFile, max(ImpTime) as TimeGroup, max(ImpTime) as ImpTime from people group by ImpFile union all select p.Name, p.ImpFile, ldr.TimeGroup, p.ImpTime from people p inner join grp ldr
Output:
NAME IMPFILE IMPTIME TIMEGROUP (null) Import12 2012-05-16 09:55:37.3870000 2012-05-16 09:55:37.3870000 Bart Import12 2012-05-16 09:55:37.3870000 2012-05-16 09:55:37.3870000 John Import12 2012-05-16 09:55:37.3840000 2012-05-16 09:55:37.3870000 Sasha Import12 2012-05-16 09:55:37.3850000 2012-05-16 09:55:37.3870000 (null) Imp01 2012-05-16 09:54:02.4780000 2012-05-16 09:54:02.4780000 Ann Imp01 2012-05-16 09:54:02.4780000 2012-05-16 09:54:02.4780000 Mark Imp01 2012-05-16 09:54:02.4770000 2012-05-16 09:54:02.4780000 Sam Imp01 2012-05-16 09:54:02.4770000 2012-05-16 09:54:02.4780000
Live test: http://www.sqlfiddle.com/#!3/c7859/25