Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a method which abuses the row_number function. Please check the comments for some explanation.</p> <pre><code>;with Seq as ( -- Create a master sequence of events -- Trust the date column to be accurate (don't match a Begin to an earlier End) select id, activitytype, beginend , coalesce(businesskey, '') as businesskey -- Needed to match nulls as equal , [date], technician, note , row_number() over (partition by technician, businesskey, activitytype order by [date], beginend, id) as rownumber from TimeEntry ) select b.id as BeginID , e.id as EndID , coalesce(b.technician, e.technician) as Technician , coalesce(b.businesskey, e.businesskey) as BusinessKey , coalesce(b.activitytype, e.activitytype) as ActivityType , coalesce(convert(char(10), b.[date], 103), 'Incomplete') as BeginDate , coalesce(convert(char(10), e.[date], 103), 'Incomplete') as EndDate , coalesce(convert(char(5), b.[date], 108), 'Incomplete') as BeginTime , coalesce(convert(char(5), e.[date], 108), 'Incomplete') as EndTime , b.note as BeginNote , e.note as EndNote from (select * from Seq where beginend = 'Begin') b -- Get all Begins full outer join (select * from Seq where beginend = 'End') e -- Get all Ends on b.technician = e.technician and b.businesskey = e.businesskey and b.activitytype = e.activitytype and b.rownumber = e.rownumber - 1 -- Match a Begin with only the very next End of that type order by coalesce(b.[date], e.[date]) , coalesce(b.id, e.id) , coalesce(b.technician, e.technician) , coalesce(b.businesskey, e.businesskey) , coalesce(b.activitytype, e.activitytype) </code></pre> <p>And the <a href="http://sqlfiddle.com/#!3/1fc2b/1" rel="nofollow">SQL Fiddle</a> should anyone want the DDL or demo.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload