0

It's possible to create on SQL, a query to schedule my IT Calendar? I have this table:

IT Technician | Date
John          | 21-09-2014
Mark          | 20-09-2014

To control my IT Schedule I want a list of the schedule of my IT Guys.

           19-09-2014     20-09-2014     21-09-2014
John          Free           Free         Occupied
Mark          Free          Occupied        Free
Manuel        Free           Free           Free
Ricardo       Free           Free           Free

It's possible on a SQL Query?

Thank you in advance, Tiago Januário

xQbert
  • 34,733
  • 2
  • 41
  • 62
  • 2
    Yes it's possible, however syntax will depend on flavor of SQL (what RDBMS and version?). if `PIVOT` is supported than yes. otherwise a pivot equivalent would be needed. (MSFT Access would call it a cross tab query) – xQbert Sep 26 '14 at 15:20

1 Answers1

0

you need to dynamic SQL using PIVOT

DECLARE @cols nvarchar(max)
DECLARE @query nvarchar(max)
set @cols = ( STUFF( ( select ',[' + Date + ']' from Table1 
                       for xml path ('')),1,1,''))

set @query = 'select  '+ @cols +' FROM 
(select *  from Table1 )T
pivot
( max([IT Technician]) for Date in ( ' + @cols + ')
)p'


exec sp_executesql @query
radar
  • 13,270
  • 2
  • 25
  • 33