• 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
    Computer Keyes

    Fax Directly from your IBM i

    KeyesFax is a full function automated IBM i fax system. Spooled files are burst by fax number and auto transmitted with overlays.  It combines both a send and receive facsimile processing system with a complete image package.

    The fax software will edit, send, receive, display, print, and track fax documents or images using any standard IBM i without additional expensive hardware, software or subscriptions.

    Computer Keyes has been developing Software Solutions since 1978!

    www.computerkeyes.com

    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

    Presenting Vertical Data Horizontally Admin Alert: Major And Minor IBM i Power System Upgrades

    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

  • IBM Gets Bob 1.0 Off The Ground
  • You Store The Crown Jewels In A Safe, Not In A Bucket
  • More Power Systems Withdrawals, And Some From Red Hat, Too
  • Price Increases Are Here, Or Pending, And For Sure For Memory
  • IBM i PTF Guide, Volume 28, Number 9
  • After A Few Short Years, VS Code Passes Rational Developer for i
  • Why Logical Replication Has Become The New Standard for IBM i HA/DR
  • Guru: Managing The Lifecycle Of Your Service Programs – Updates Without Chaos
  • IT Spending Forecast Keeps Going Up And Up, But It Won’t Go Away
  • IBM i PTF Guide, Volume 28, Number 8

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