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

  • Home
  • SEARCH
  • 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 9235561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:04:41+00:00 2026-06-18T07:04:41+00:00

I have a large data set in a denormalized format. Here is an example

  • 0

I have a large data set in a denormalized format. Here is an example of the column names:

foreign_key_ID, P1, P2, P3, P4, P5…. D1, D2, D3…. etc..

These fields all contain similar type of data.

I need to normalize this into my existing table structure:

insert into new_table (id, name, index)
select foreign_key_id, P1, 1
from denormalized_table;

But that means that I need to run separate queries for each field in my denormalized table, just changing a few things:

insert into new_table (id, name, index)
select foreign_key_id, P2, 2
from denormalized_table;

This is getting tedious considering how many of these fields I have.

Is there a way this can be automated into a single operation? I.e.: iterate through the fields (I don’t mind creating a list of eligible fields once, somewhere), pull off the last digit of that field name (ie “1” in “P1” and “2” for “P2”) use the field name and the extracted index # in the sub-select.

  • 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-18T07:04:42+00:00Added an answer on June 18, 2026 at 7:04 am

    Here’s a start:

    SELECT column_name, substr(column_name,2) AS `index`
      FROM information_schema.columns
     WHERE table_schema = 'mydatabasename'
       AND table_name = 'denormalized_table'
       AND column_name REGEXP '^[PD][0-9]+$'
     ORDER BY column_name
    

    You can modify the select list in that statement, to have MySQL generate statements for you:

    SELECT CONCAT('INSERT INTO new_table (id, name, `index`) SELECT foreign_key_id, '
             ,column_name,', ',substr(column_name,2)
             ,' FROM denormalized_table ;') AS stmt
      FROM information_schema.columns
     WHERE table_schema = 'mydatabasename'
       AND table_name = 'denormalized_table'
       AND column_name REGEXP '^[PD][0-9]+$'
     ORDER BY column_name
    

    The output from that would be a set of MySQL INSERT statements that you could then execute.


    If the number of rows and total size of the data to be inserted is not too large, you could and you want to get the whole conversion done in “one operation”, then you could generate a single INSERT INTO … SELECT statement, using the UNION ALL operator. I would get the majority of the statement like this:

    SELECT CONCAT('UNION ALL SELECT foreign_key_id, '
             ,column_name,', ',substr(column_name,2)
             ,' FROM denormalized_table ') AS stmt
      FROM information_schema.columns
     WHERE table_schema = 'mydatabasename'
       AND table_name = 'denormalized_table'
       AND column_name REGEXP '^[PD][0-9]+$'
     ORDER BY column_name
    

    I would take the output from that, and replace the very first UNION ALL with an INSERT INTO .... That would give me a single statement to run to get the whole conversion done.

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

Sidebar

Related Questions

I have a large data set in the following format: In total, there are
I have a very large data set that contains multiple groups. They all contain
I have a large data set coming back in JSON format such that I
I have a large data set with files containing three also large single column
I have a large data set with 11 columns and 100000 rows (for example)
I have a large data frame with the following fields (example data). #dput(data) gives...
I have a large data set and I want to write a custom merge
I have a large data set that I'm working with in excel. About 1000+
Interpolating Large Datasets I have a large data set of about 0.5million records representing
I have a very large possible data set that I am trying to visualize

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.