• 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
    ARCAD Software

    Embrace VS Code for IBM i Development

    The IBM i development landscape is evolving with modern tools that enhance efficiency and collaboration. Ready to make the move to VS Code for IBM i?

    Join us for this webinar where we’ll showcase how VS Code can serve as a powerful editor for native IBM i code and explore the essential extensions that make it possible.

    In this session, you’ll discover:

    • How ARCAD’s integration with VS Code provides deep metadata insights, allowing developers to assess the impact of their changes upfront.
    • The role of Git in enabling seamless collaboration between developers using tools like SEU, RDi, and VS Code.
    • Powerful extensions for code quality, security, impact analysis, smart build, and automated RPG conversion to Free Form.
    • How non-IBM i developers can now contribute to IBM i projects without prior knowledge of its specifics, while ensuring full control over their changes.

    The future of IBM i development is here. Let ARCAD be your guide!

    Watch Now

    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

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

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