0

I have a output from select as below

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

Which I need to be converted as below

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

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

How can I modify my select query to group the records under husband , wife , kid1 , kid2 , kid 3 and kid 4 ?

3
  • what is your query you want to modify ? Commented Feb 3, 2013 at 17:06
  • Your question is very unclear, can you be more specific? Commented Feb 3, 2013 at 17:07
  • u dont need kid3 and kid4 ? Commented Feb 3, 2013 at 17:08

1 Answer 1

1

Your question is not exactly clear but it seems like you want to unpivot the existing columns and then pivot the values in the user_type column.

If that is the case, then you will want to use a UNION ALL to unpivot the data and then apply an aggregate function with a CASE expression to pivot to get the final result:

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

See SQL Fiddle with Demo. Your sample data does not have many values, but the result will be similar to this:

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

Note: I included additional columns to unpivot but if you only want the fname you would only include those columns in the subquery

Edit #1, if you need to keep the order of the data based on the columns in the original table, then you can add a column to the UNION ALL queries with the sort order. You can then use that column in an ORDER BY. So the query will be:

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

See SQL Fiddle with Demo

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks but the query is ordering the col_NAME in alphabetical order , Can u please help me in skipping this ordering.
@oldrock I don't have an order by on the query. What do you expect the result to be?
the column name should be ordered as it was present in the table suppose prefix is first then col_NAME should start with prefix followed by fname , mname , lname , suffix , nick_name,company .. etc
@oldrock all you need to do is add a column to the UNION ALL that will determine the sort order see this demo -- sqlfiddle.com/#!2/08b5a/18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.