2012-12-20 84 views
3

我對SQL相當陌生,但相信我在發佈之前已經搜索過幫助。SQL數據透視日期列?

我有一個查詢,它返回分配給作業的人員列表,這些作業也有不同的長度,分配給這些作業的人員工作的長度不同。

我想要做的是轉換什麼是類似記錄的列表,只有變量的變化是日期,以及一些如何使這些數據轉動,使日期成爲列標題,行代表BOOL是/否。

這是我目前找回的數據。 JSON編碼

{"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"}]}

什麼,我想得到的回覆是:

{"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"}]}

我敢肯定,這是某種形式的PIVOT查詢的,但我不能得到它的工作。

感謝

+0

你試圖做到這一點的SQL?你使用的是什麼rdbms?你可以發佈表格中的數據而不是JSON格式嗎? – Taryn

+0

待機........它有點複雜的連接查詢。 –

+0

我基本上使用PHP訪問遠程SQL服務器,然後在JSON格式顯示在iPhone應用程序。我無法真正看到一種簡單的方法來將數據庫轉儲爲不受我管理的數據庫。 –

回答

10

如果你要運行在SQL Server此查詢,那麼你可以使用PIVOT功能:

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 

SQL Fiddle with Demo

或者你可以使用一個聚合函數和CASE聲明:

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 

SQL Fiddle with Demo

均可以得到結果:

|    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 | 

以上的偉大工程,如果你知道移調值,但如果你不這樣做,那麼你可以使用動態SQL與此類似:

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) 

SQL Fiddle with Demo

+0

感謝您的回覆,但問題是我無法對日期進行硬編碼,它們是動態的。所以我做了兩個查詢,並在iPhone中對結果進行了整理。 –

+2

@SimonUnsworth看到我的編輯,我添加了一個動態sql版本的查詢 – Taryn

+0

我不得不再次做類似的事情,我會嘗試動態SQL。它可以在所有服務器上運行,還是需要特殊插入? –