I'm fairly new to SQL but believe me I have searched for help before posting this.
I have a query which returns a list of people assigned to jobs, also the jobs have varying length and people who are assigned to those jobs are working different lengths.
What I am trying to do is convert what is a list of similar records with the only variable changing is the date, and some how pivot this data so that the dates become column headings and the rows represent a BOOL yes/no.
This is the data I'm getting back currently. JSON encoded
{"results":[{"role":"Vision Supervisor","familyname":"Unsworth","givenname":"Simon","skill":"10","level":"Telegenic Staff","id":"664","date":"2013-03-27"},{"role":"Vision Supervisor","familyname":"Unsworth","givenname":"Simon","skill":"10","level":"Telegenic Staff","id":"664","date":"2013-03-26"},{"role":"Vision Supervisor","familyname":"Unsworth","givenname":"Simon","skill":"10","level":"Telegenic Staff","id":"664","date":"2013-03-25"},{"role":"Vision Supervisor","familyname":"Unsworth","givenname":"Simon","skill":"10","level":"Telegenic Staff","id":"664","date":"2013-03-24"}]}
and what I would like to get back is:
{"results":[{"role":"Vision Supervisor","familyname":"Unsworth","givenname":"Simon","skill":"10","level":"Telegenic Staff","id":"664","2013-03-27":"YES","2013-03-26":"YES","2013-03-25":"YES","2013-03-24":"YES"}]}
I'm sure this is some kind of PIVOT query but I cant get it to work.
Thanks
If you already know which columns to create in pivot table, you can use a CASE statement to create a pivot table. However, to create dynamic pivot tables in MySQL, we use GROUP_CONCAT function to dynamically transpose rows to columns, as shown below.
Introduction to SQL Server PIVOT operator You follow these steps to make a query a pivot table: First, select a base dataset for pivoting. Second, create a temporary result by using a derived table or common table expression (CTE) Third, apply the PIVOT operator.
The following syntax illustrates the use of PIVOT in SQL Server: SELECT <non-pivoted column>, <list of pivoted column> FROM (<SELECT query to produces the data>) AS <alias name> PIVOT ( <aggregation function>(<column name that will aggregated>)
If you are going to be running this query in SQL Server, then you can use the PIVOT
function:
select *
from
(
select role, familyname, givenname, skill,
level, id, date, 'Y' flag
from yourtable
) src
pivot
(
max(flag)
for date in ([2013-03-27], [2013-03-26],
[2013-03-25], [2013-03-24])
) piv
See SQL Fiddle with Demo
Or you can use an aggregate function and a CASE
statement:
select role, familyname, givenname, skill,
level, id,
max(case when date = '2013-03-27' then flag end) '2013-03-27',
max(case when date = '2013-03-26' then flag end) '2013-03-26',
max(case when date = '2013-03-25' then flag end) '2013-03-25',
max(case when date = '2013-03-24' then flag end) '2013-03-24'
from
(
select role, familyname, givenname, skill,
level, id, date, 'Y' flag
from yourtable
) src
group by role, familyname, givenname, skill,
level, id
See SQL Fiddle with Demo
Both give the result:
| ROLE | FAMILYNAME | GIVENNAME | SKILL | LEVEL | ID | 2013-03-27 | 2013-03-26 | 2013-03-25 | 2013-03-24 |
----------------------------------------------------------------------------------------------------------------------------------
| Vision Supervisor | Unsworth | Simon | 10 | Telegenic Staff | 664 | Y | Y | Y | Y |
The above works great if you know the values to transpose, but you if you don't then you can use dynamic sql similar to this:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(convert(char(10), date, 120))
from yourtable
group by date
order by date desc
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT role, familyname, givenname, skill,
level, id,' + @cols + ' from
(
select role, familyname, givenname, skill,
level, id, date, ''Y'' flag
from yourtable
) x
pivot
(
max(flag)
for date in (' + @cols + ')
) p '
execute(@query)
See SQL Fiddle with Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With