I've looking everywhere and have not finding anything useful.
I have a table the captures assistance for employees.
The table that looks like this:
ID | DATE | ATTENDANCE
________________
2524 | 20121001 | ASISTANCE
2525 | 20121001 | ABSCENCE
2526 | 20121001 | ASISTANCE
2527 | 20121001 | ASISTANCE
2524 | 20121002 | ASISTANCE
2525 | 20121002 | ABSCENCE
2526 | 20121002 | ASISTANCE
2527 | 20121002 | ASISTANCE
2524 | 20121003 | ASISTANCE
2525 | 20121003 | DAY OFF
2526 | 20121003 | DAY OFF
2527 | 20121003 | ASISTANCE
And I want a query that returns a table that will look like this:
ID | 20121001 | 20121002 | 20121003
________________
2524 | ASISTANCE | ASISTANCE | ASISTANCE
2525 | ABSCENCE | ABSCENCE | DAY OFF
2526 | ASISTANCE | ASISTANCE | ASISTANCE
2527 | ASISTANCE | ASISTANCE | DAY OFF
I tried individual querys and joining them, but since they are to many dates it takes too much to do so.
How can I do it that is efficient and can be stored into a view or function??
Actually, this can be done with the
PIVOT
function. There are two ways to usePIVOT
, either static or dynamic.Static Pivot, you will hard-code the values to turn into columns:
A dynamic
PIVOT
will get the list of values at run-time and will be more flexible if the list is changing:Both will give the same result:
It would be easier to get the data and process it in a server-side language like PHP. It would then be a trivial matter to build the array:
Then:
You now have a tab-separated table.