Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9228137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:15:00+00:00 2026-06-18T05:15:00+00:00

I have a output from select as below | s.no | user_id | user_type

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:15:01+00:00Added an answer on June 18, 2026 at 5:15 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Query which produces the output below; SELECT TBLUSERS.USERID, TBLUSERS.ADusername, TBLACCESSLEVELS.ACCESSLEVELID,
I have this code below, with which I select first all id's from table
I am creating a csv file as given below $user_rep=Engine_Db_Table::getDefaultAdapter()->select() ->from('engine4_users',array('user_id','email','displayname')) ->query()->fetchAll(); $csv_output .=
I have output from grep I'm trying to clean up that looks like: <words>Http://www.path.com/words</words>
i have output from a server like [alex, \ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd] i want to convert it
I am trying to debug a segfault, and I have this output from gdb:
i have an xml output from a webservice in a variable results it looks
I have a directory OUTPUT where I have the output files from a Map
I have an array of output from a database. I am wondering what the
I have some qml that acts as the output from the application (kind of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.