• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • First Normal Form On The Fly

    July 10, 2013 Ted Holt

    Converting normalized data into a repeating group is a common requirement, and we have discussed that topic several times in Four Hundred Guru, even in this issue. Sometimes the need arises to convert data in the other direction, i.e., to convert a repeating group to a normalized form. This is a handy technique to know, and it’s easy.

    Suppose you’ve been given a spreadsheet with 13 columns of data. The first column is an item number. The next 12 columns are replacement costs for each month. You’ve been asked to load the data into a normalized table (physical file) of three columns (fields):

    1. Item number
    2. Month
    3. Cost

    That is, you need to convert this:




    Item

    Item

    January

    February

    . . .

    December

    A

    5.41

    5.41

    . . .

    6.18

    B

    3.80

    4.50

    . . .

    12.55

    into this:




    Item

    Item

    Month

    Cost

    A

    2012-01-01

    5.41

    A

    2012-02-01

    5.41

    . . .

     

     

    A

    2012-12-01

    6.18

    B

    2012-01-01

    3.80

    B

    2012-02-01

    4.50

    . . .

     

     

    B

    2012-12-01

    12.55

    To convert columns to rows, use UNION ALL.

    Assuming you’ve copied the spreadsheet data into a table called ITEMCOSTS and will load table COSTHIST, here’s the SQL you need.

    insert into costhist
    
       select c.ItemNumber, '2012-01-01', c.cost01
         from itemcosts as c
       union all
       select c.ItemNumber, '2012-02-01', c.cost02
         from itemcosts as c
       union all
    
    ( . . . etc. for months 3 - 11 . . .)
    
       union all
       select c.ItemNumber, '2012-12-01', c.cost12
         from itemcosts as c
       order by 1,2
    

    The expression requires a SELECT for each “bucket”. In this case, there are 12 select statements because there are 12 months.

    UNION would produce the same result as UNION ALL, but UNION ALL performs much better because it does not check for duplicate rows.

    I would hate to use this technique with the file that FHG reader David told me about recently. It has six repeating fields, each one of which has 90 buckets. (Yes, 90!) But I suppose it could be done.

    RELATED STORY

    Presenting Vertical Data Horizontally



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Midrange Dynamics North America

    Git up to speed with MDChange!

    Whether you are managing large Git repositories for IBM i applications or you’re orchestrating smaller repositories, Midrange Dynamics has solutions to boost Git performance for IBM i.

    Git workflow in MDChange is specifically designed for IBM i, optimizing repository management, testing, and deployments for greater productivity, flexibility, and scalability. MDChange supercharges performance for GitHub, GitLab, Bitbucket, and Azure Repos.

    Learn More.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Linoma Software:  FREE Webinar: Conquer Compliance Requirements. July 18.
    Cybernetics:  Ditch the tape backup? Up to 4.3 TB/hr! Start saving now!
    Abacus Solutions:  Qualified IBM i users eligible for free pair of running shoes

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ The Reg: More News From ITJ EIC Timothy Prickett Morgan

    Enterprise App Store Approach to Spread for Mobile Apps, Study Says What Is IBM Going To Do With Its Systems Business?

    Leave a Reply Cancel reply

Volume 13, Number 13 -- July 10, 2013
THIS ISSUE SPONSORED BY:

WorksRight Software
PowerTech
American Top Tools

Table of Contents

  • Presenting Vertical Data Horizontally
  • First Normal Form On The Fly
  • Admin Alert: Major And Minor IBM i Power System Upgrades

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • What You Will Find In IBM i 7.6 TR1 and IBM i 7.5 TR7
  • Three Things For IBM i Shops To Consider About DevSecOps
  • Big Blue Converges IBM i RPG And System Z COBOL Code Assistants Into “Project Bob”
  • As I See It: Retirement Challenges
  • IBM i PTF Guide, Volume 27, Number 41
  • Stacking Up Power11 Entry Server Performance To Older Iron
  • Big Blue Boosts IBM i Support In Instana, Adds Tracing
  • It Is Time To Tell Us What You Are Thinking And Doing
  • IBM i PTF Guide, Volume 27, Number 40
  • The GenAI Boom Is Only Slightly Louder Than The Dot Com Boom

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle