你的問題是不完全清楚,但好像你要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
什麼是你要修改您的查詢? –
你不需要kid3和kid4嗎? –
你的問題很不清楚,你可以更具體嗎? – Jonast92