Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using CTE's and Row_Number() I think I can return what you are looking for. The key is to only perform your full outer by joining on adajacent rows based upon an ordering of technician, businesskey, activitytype. Anyway here it is:</p> <pre><code>WITH EventsCTE AS ( select id, technician,businessKey,activityType, CAST(date as date) As date, dateSubmitted, beginEnd, note, ROW_NUMBER() OVER (Partition BY technician, businesskey, activityType ORDER BY dateSubmitted) as rowN from TimeEntry ), BeginEventsCTE AS ( select id, technician,businessKey,activityType, CAST(date as date) As date, dateSubmitted, note, rowN from EventsCTE WHERE beginEnd = 'Begin' ), EndEventsCTE AS ( select id, technician,businessKey, activityType, CAST(date as date) As date, dateSubmitted, note, rowN from EventsCTE WHERE beginEnd = 'End' ) SELECT B.id, E.id As EndId, COALESCE(B.activityType ,E.activityType ,'Not Available') AS ActivityType, COALESCE(convert(VARCHAR(12), B.[date], 103), 'Incomplete') As EventDate, COALESCE(convert(VARCHAR(12), E.[date], 103), 'Incomplete') As EndDate, COALESCE(CONVERT(VARCHAR(5) , B.dateSubmitted , 108) , 'Incomplete') As StartTime, COALESCE(CONVERT(VARCHAR(5) , E.dateSubmitted , 108) , 'Incomplete') As EndTime, COALESCE(B.Note, E.note, '') as Note, COALESCE(B.technician, E.technician, 'Not Available') As Technician --, B.rowN, E.rowN FROM BeginEventsCTE B FULL OUTER join EndEventsCTE E ON B.technician = E.technician AND B.businessKey = E.businessKey AND B.activityType = E.activityType AND B.date = E.date AND B.rowN = E.rowN - 1 ORDER BY COALESCE(B.Technician, E.Technician), COALESCE(b.id,E.id) </code></pre> <p>And here's the results:</p> <p><img src="https://i.stack.imgur.com/qHiNI.jpg" alt="query results"></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. 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.
 

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