0

以下のようにselectからの出力があります

| 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

その場合は、 a を使用しUNION 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 に答える