2013-02-03 65 views
0

我從選擇一個輸出如下如何根據特定的行值將行轉換爲列?

| s.no | user_id | user_type | user_group | prefix | fname | mname | lname | suffix |nick_name | company | department | designation_title | industry | dob  | nationality | passport_number | photograph | mobile | email | permanent_address | temporary_address | bbm_p |t_number_arrival | departure_date_time | depart_airlines | flight_number_departure | 

| 17 |  0 | Husband |   23 | sasas | asd |  |  |  |   |   |   |     |   |0000-00-00 |    |     |   |  0 | asdas |     | sadx    | asd| 0000-00-00 00:00:00 |     |       | 
| 18 |  0 | wife  |   23 |  |  |  |  |  |   |   |   |     |   | 0000-00-00 |    |     |   |  0 |  |     |     |  | 0000-00-00 00:00:00 |     |       | 
| 19 |  0 | kid1  |   23 |  |  |  |  |  |   |   |   |     |   | 0000-00-00 |    |     |   |  0 |  |     |     | | 0000-00-00 00:00:00 |     |       | 
| 20 |  0 | kid2  |   23 |  |  |  |  |  |   |   |   |     |   | 0000-00-00 |    |     |   |  0 |  |     |     |  | 0000-00-00 00:00:00 |     |       | 
| 21 |  0 | kid3  |   23 |  |  |  |  |  |   |   |   |     |   | 0000-00-00 |    |     |   |  0 |  |     |     | | 0000-00-00 00:00:00 |     |       | 
| 22 |  0 | kid4  |   23 |  |  |  |  |  |   |   |   |     |   | 0000-00-00 |    |     |   |  0 |  |     |     |  | 0000-00-00 00:00:00 |     |       | 
| 23 |  0 | Husband |   24 | sasas | asd |  |  |  |   |   |   |     |   | 0000-00-00 |    |     |   |  0 | asdas |     | sadx    | asd| 0000-00-00 00:00:00 |     |       | 
| 24 |  0 | wife  |   24 |  |  |  |  |  |   |   |   |     |   | 0000-00-00 |    |     |   |  0 |  |     |     | | 0000-00-00 00:00:00 |     |       | 
| 25 |  0 | kid1  |   24 |  |  |  |  |  |   |   |   |     |   | 0000-00-00 |    |     |   |  0 |  |     |     | 

,我需要被轉換爲以下

| husband | wife | kid1 | kid2 | kid3 | kid4 

    sasas |  |  |  |  | 
    asd |  |  |  |  | 

如何修改我的選擇查詢到組記錄下 丈夫,妻子,KID1 ,kid2,kid 3和kid 4?

+0

什麼是你要修改您的查詢? –

+0

你不需要kid3和kid4嗎? –

+0

你的問題很不清楚,你可以更具體嗎? – Jonast92

回答

1

你的問題是不完全清楚,但好像你要unpivot的現有列,然後支點user_type列中的值。

如果是這樣的話,那麼你將要使用UNION ALL來unpivot的數據,然後應用聚合函數有CASE表達樞轉以得到最終結果:

select user_id, 
    user_group, 
    col_name, 
    max(case when user_type = 'Husband' then value end) as Husband, 
    max(case when user_type = 'wife' then value end) as Wife, 
    max(case when user_type = 'kid1' then value end) as Kid1, 
    max(case when user_type = 'kid2' then value end) as Kid2, 
    max(case when user_type = 'kid3' then value end) as Kid3, 
    max(case when user_type = 'ki4=d4' then value end) as Kid4 
from 
(
    select user_id, user_type, user_group, 
     'prefix' as col_name, prefix as value 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'fname' as col_name, fname as value 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'mname' as col_name, mname as value 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'lname' as col_name, lname as value 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'suffix' as col_name, suffix as value 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'nick_name' as col_name, nick_name as value 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'company' as col_name, company as value 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'department' as col_name, department as value 
    from yourtable 
) src 
group by user_id, user_group, col_name 

SQL Fiddle with Demo。您的樣本數據沒有多少價值,但結果將類似於此:

| USER_ID | USER_GROUP | COL_NAME | HUSBAND | WIFE | KID1 | KID2 | KID3 | KID4 | 
-------------------------------------------------------------------------------------------- 
|  0 |   23 | company | (null) | (null) | (null) | (null) | (null) | (null) | 
|  0 |   23 | department | (null) | (null) | (null) | (null) | (null) | (null) | 
|  0 |   23 |  fname |  asd | (null) | (null) | (null) | (null) | (null) | 
|  0 |   23 |  lname | (null) | (null) | (null) | (null) | (null) | (null) | 
|  0 |   23 |  mname | (null) | (null) | (null) | (null) | (null) | (null) | 
|  0 |   23 | nick_name | (null) | (null) | (null) | (null) | (null) | (null) | 
|  0 |   23 |  prefix | sasas | (null) | (null) | (null) | (null) | (null) | 
|  0 |   23 |  suffix | (null) | (null) | (null) | (null) | (null) | (null) | 
|  0 |   24 | company | (null) | (null) | (null) | (null) | (null) | (null) | 
|  0 |   24 | department | (null) | (null) | (null) | (null) | (null) | (null) | 
|  0 |   24 |  fname |  asd | (null) | (null) | (null) | (null) | (null) | 
|  0 |   24 |  lname | (null) | (null) | (null) | (null) | (null) | (null) | 
|  0 |   24 |  mname | (null) | (null) | (null) | (null) | (null) | (null) | 
|  0 |   24 | nick_name | (null) | (null) | (null) | (null) | (null) | (null) | 
|  0 |   24 |  prefix | sasas | (null) | (null) | (null) | (null) | (null) | 
|  0 |   24 |  suffix | (null) | (null) | (null) | (null) | (null) | (null) | 

注:我包括附加列UNPIVOT,但如果你只希望fname你只包括在子查詢的那些列

編輯#1,如果您需要根據原始表中的列保留數據的順序,則可以使用排序順序將列添加到UNION ALL查詢中。然後,您可以在ORDER BY中使用該列。所以查詢將是:

select user_id, 
    user_group, 
    col_name, 
    max(case when user_type = 'Husband' then value end) as Husband, 
    max(case when user_type = 'wife' then value end) as Wife, 
    max(case when user_type = 'kid1' then value end) as Kid1, 
    max(case when user_type = 'kid2' then value end) as Kid2, 
    max(case when user_type = 'kid3' then value end) as Kid3, 
    max(case when user_type = 'ki4=d4' then value end) as Kid4 
from 
(
    select user_id, user_type, user_group, 
     'prefix' as col_name, prefix as value 
     , 1 as sortorder 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'fname' as col_name, fname as value 
     , 2 as sortorder 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'mname' as col_name, mname as value 
     , 3 as sortorder 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'lname' as col_name, lname as value 
     , 4 as sortorder 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'suffix' as col_name, suffix as value 
     , 5 as sortorder 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'nick_name' as col_name, nick_name as value 
     , 6 as sortorder 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'company' as col_name, company as value 
     , 7 as sortorder 
    from yourtable 
    union all 
    select user_id, user_type, user_group, 
     'department' as col_name, department as value 
     , 8 as sortorder 
    from yourtable 
) src 
group by user_id, user_group, col_name 
order by user_group, sortorder 

SQL Fiddle with Demo

+0

謝謝,但查詢是按字母順序排列col_NAME,請你幫我跳過這個順序。 – oldrock

+0

@oldrock我在查詢中沒有'order by'。你期望的結果是什麼? – Taryn

+0

列名應該按照表格中的順序排列假設前綴爲首,那麼col_name應以前綴開頭,後跟fname,mname,lname,後綴,nick_name,company ..等 – oldrock

相關問題