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 8920083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:10:38+00:00 2026-06-15T06:10:38+00:00

Hello i am trying to figure out how to do this i have a

  • 0

Hello i am trying to figure out how to do this i have a mysql table

| ID |    ACC_ID  | line_id | code     |
| 1  |          1 |    5960 | DCA      |
| 2  |          1 |    5960 | AAA      |
| 3  |          1 |    5960 | DDD      | 
| 4  |          1 |    5960 | DER      |
| 5  |          1 |    5054 | DCB      |
| 6  |          1 |    5054 | AAC      |
| 7  |          1 |    5011 | DDE      |
| 8  |          1 |    5012 | DEQ      |

etc the database goes down about 10000 lines

I would like to make a mysql select statement that will do this

| ACC_ID     | line_id | code     | code     | code     | code     | 
|   1        | 5960    | DCA      | AAA      | DDD      | DER      | 
|   1        | 5054    | DCB      | DER      |          |          |
|   1        | 5011    | DDE      |          |          |          |
|   1        | 5012    | DEQ      |          |          |          |

there could be up to ten codes per line_id

Now my question is it possible to make the query above using a select statement.

Thank you all for your help

  • 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-15T06:10:41+00:00Added an answer on June 15, 2026 at 6:10 am

    This is a PIVOT but MySQL does not have a PIVOT function but you can replicate it with an aggregate function and a CASE statement. MySQL also does not have the easiest ways to assign row number by group, but the following is a sample of how you could achieve this using SQL. Since you said you can have up to 10 codes per line_id I hard-coded a possible solution.:

    select acc_id,
      line_id,
      max(case when group_row_number = 1 then code end) Code1,
      max(case when group_row_number = 2 then code end) Code2,
      max(case when group_row_number = 3 then code end) Code3,
      max(case when group_row_number = 4 then code end) Code4,
      max(case when group_row_number = 5 then code end) Code5,
      max(case when group_row_number = 6 then code end) Code6,
      max(case when group_row_number = 7 then code end) Code7,
      max(case when group_row_number = 8 then code end) Code8,
      max(case when group_row_number = 9 then code end) Code9,
      max(case when group_row_number = 10 then code end) Code10
    from
    (
      select ACC_ID,
        line_id,
        code, 
        @num := if(@ACC_ID = `ACC_ID` AND @line_id = `line_id`, @num + 1, 1) as group_row_number,
        @ACC_ID := `ACC_ID` as dummy,
        @line_id := `line_id` as linedummy
      from yourtable
    ) src
    group by acc_id, line_id
    order by line_id desc
    

    See SQL Fiddle with Demo

    Result:

    | ACC_ID | LINE_ID | CODE1 |  CODE2 |  CODE3 |  CODE4 |  CODE5 |  CODE6 |  CODE7 |  CODE8 |  CODE9 | CODE10 |
    -------------------------------------------------------------------------------------------------------------
    |      1 |    5960 |   DCA |    AAA |    DDD |    DER | (null) | (null) | (null) | (null) | (null) | (null) |
    |      1 |    5054 |   DCB |    AAC | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    |      1 |    5012 |   DEQ | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    |      1 |    5011 |   DDE | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) | (null) |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to figure out how to do this. I have the style but I'd
Hello I am writing this program, and I am trying to figure out how
Hello I been trying to figure out how to set up ASP.Net forms authentication
Hello stackoverflow community ! I am trying to figure out how to architect my
Hello there im trying to figure out how i can parse RSS feeds with
I'm trying to figure out how to execute machine code stored in memory. I
Hello I've been trying to figure out generic way to log http requests in
The Quetion Hello. I'm trying to figure out the best way to use one
I'm trying to start off using FBJS, and I can't figure this out. The
This one should be simple, but I can't figure it out myself. I have

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.