title: SQL Server Timetable query
body: <p>I have a query and data that I need to generate a timetable from.</p>
  
  <p>My current query is below: </p>
  
  <pre><code>SELECT contract.lect_code code1,
         contract.coll_code code2,
         line_date,
         start_time,
         end_time,
         DATEPART(DW,line_date) AS day_number,
         datename (dw,line_date) AS nameofday,
         convert(varchar(8),start_time,108) AS start_time2,
         convert(varchar(8),end_time,108) AS end_time2
  FROM bk_line
  INNER JOIN contract ON contract.contract_no = bk_line.contract_no
  WHERE line_date BETWEEN '2013/09/23' AND '2013/09/27'
    AND coll_code = 'TEL01'
    AND bk_line_status = 'CE'
  --And lect_code = 10430973 
  </code></pre>
  
  <p>This gives me data in the following format :<img src="https://i.stack.imgur.com/jKlct.png" alt="enter image description here"></p>
  
  <p>I need a way of breaking down the times into 15 minutes slots for each code1 and line date.</p>
  
  <p>Something like this:</p>
  
  <p><code>code1 | code2 | line_date | 0900worked | 0915worked | 0930worked |</code></p>
  
  <p>where the values in <code>0900worked</code> would be <code>T</code> or <code>F</code>.</p>
  
  <p>--</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I need to group the periods by code1 and line_date. Therefore in the example above, <code>1045096</code> worked two sessions on <code>2013/09/25</code>. I need both sessions to appear on the same line with all periods calculated.</p>