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

如何修改我的选择查询以将记录分组在丈夫、妻子、孩子 1、孩子 2、孩子 3 和孩子 4 下?

4

1 回答 1

1

您的问题并不完全清楚,但您似乎想要取消透视现有列,然后旋转列中的值user_type

如果是这种情况,那么您将需要使用 aUNION ALL来取消透视数据,然后应用带有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) |

注意:我包括了额外的列来取消透视,但如果你只想要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

于 2013-02-03T17:17:11.410 回答