• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • How To Insert With A Common Table Expression

    August 16, 2016 Hey, Ted

    I have to merge some new item numbers into our item master file. The problem I’ve run into is that there is more than one record for some items. None of the examples I found on the Web work for me. How do I write an SQL statement to copy all records from one file to another avoiding duplicates on part number?

    –Trent

    This is a new wrinkle on a challenge about which I have written before, namely the need to select only one row (record) to represent a group. (See the Related Stories below.) The wrinkle is that not all database management systems handle common table expressions within inserts in the same way. I’ll come back to that in a minute.

    First, let’s set up the problem. Assume an item master file.

    create table items
      ( Item_ID        char (  6   ) primary key,
        Description    char ( 20   ),
        Cost           dec  (  7,2 ),
        Price          dec  (  7,2 ),
        Type           char (  1   ),
        Class          char (  1   ),
        UnitOfMeasure  char (  2   ) )
    

    Here’s the load file:

    create table dataload
      ( Item_number    char (  6   ),
        Description    char ( 20   ),
        Cost           dec  (  7,3 ),
        Price          dec  (  7,2 ),
        DateSold       dec  (  8,0 ) );
    
    insert into dataload values
    ('AT-771', 'Widget'   , 177.50 ,   258.77 , 20160801),
    ('BR-549', 'Samples'  ,  55.04 ,   135.80 , 20160801),
    ('AT-771', '#4 Widget', 738.22 ,  1386.36 , 20160802)
    

    The challenge is to load these items into the item master file. Where there are two or more rows for an item, we pick the one with the latest date.

    Here’s one method.

    insert into items
          (item_id, description, cost, price)
    select a.item_number, a.description, a.cost, a.price
      from dataload as a
      join (select item_number, max(DateSold) as DateSold
              from dataload
             group by item_number) as b
        on (a.item_number, a.DateSold) =
           (b.item_number, b.DateSold)
    

    There are two SELECT statements within this INSERT. The system runs the second SELECT–the one in parentheses following JOIN–first to build a temporary table of each distinct item number and the latest date that that item number was sold. This temporary table is joined to the original data on item number and date sold, yielding the desired row for each item. The result set from the join is inserted into the table.

    This will not work if there are two sales on the same date for one item. Here’s another method that does not suffer from that limitation.

    insert into items
          (item_id, description, cost, price)
    with Temp as (
    select d.*,
           row_number() over (partition by d.item_number
                                  order by d.datesold    desc) as rn
      from dataload as d)
    select item_number, description, cost, price
      from temp where rn = 1
    

    The common table expression, TEMP, uses the ROW_NUMBER function to assign a sequential number to each row within each item number. Think of PARTITION BY as a control break. ORDER BY is a sort, of course. The system sorts the data by item number and numbers each row in the result set. Numbering restarts at 1 for each item.

    The second SELECT retrieves the number 1 row for each item, preventing duplicate items from being loaded into the ITEMS table.

    What I wanted to point out to you is the syntax to use a common table expression within an insert.

    INSERT . . .
    WITH . . .
    SELECT . . .
    

    Be aware that the syntax for INSERT that uses a common table expression is different on other systems. I suppose that all database management systems will eventually support all the variations in their ongoing attempt to lure customers from other platforms.

    Ted Holt welcomes your comments and questions. Email him through the IT Jungle Contacts page.

    RELATED STORIES

    Let One Row Represent A Group, Take Three

    Let One Row Represent a Group, Take 2

    Let One Row Represent a Group

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags:

    Sponsored by
    Lightedge

    Your IBM Power Cloud Strategy Should Start With “Why?”

    Moving IBM i to the cloud isn’t just an infrastructure decision.

    A hardware refresh, data center exit, IBM i skills gap, disaster recovery requirement, or broader cloud initiative can each lead to a very different architecture.

    At Lightedge, we start by understanding what you’re trying to accomplish. Then we look at the workload, application dependencies, resiliency requirements, operating model, and long-term strategy to determine where that workload belongs.

    Managed IBM Power cloud? IBM Power Virtual Server? DR in the cloud? A hybrid architecture connecting IBM Power with AWS or Azure?

    There isn’t one right answer for every IBM i environment.

    Start with the business requirement. Understand the workload. Then decide where it belongs.

    Explore IBM Power cloud, managed services, and hybrid solutions from Lightedge.

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Sponsored Links

    System i Developer:  RPG & DB2 Summit - October 4-6 2016 in Chicago. Register now!
    COMMON:  Open Source and Systems Management at the COMMON Forum. August 24-25 in Chicago.
    Four Hundred Monitor Calendar:  Latest info on national conferences, local events, & Webinars

    Overlaid Packed Data In Data Structures Assorted DB2 for i Questions

    Leave a ReplyCancel reply

Volume 16, Number 18 -- August 16, 2016
THIS ISSUE SPONSORED BY:

WorksRight Software
Baseline Data Services
COMMON

Table of Contents

  • Overlaid Packed Data In Data Structures
  • How To Insert With A Common Table Expression
  • Assorted DB2 for i Questions

Content archive

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

Recent Posts

  • MIMIX 11 Ships As Tech Outages Dominate Headlines
  • Could You Recover Tomorrow? A CISO’s Perspective On IBM i Security In 2027
  • Guru: RPG’s New %INFO BIF. Is It Time to Retire the SDS?
  • Big Blue Backs Off On Power Memory Redundancy As Memory Prices Soar
  • IBM i PTF Guide, Volume 28, Number 42
  • IBM i Delivers New Development Tooling With 2026 Fall Tech Refreshes
  • Big Blue Says AI Is Not Behind The Big Uptick In IBM i Security Alerts
  • Guru: Web Service With Pagination
  • The GenAI Revolution And Product Shortages Send IT Budgets Skyward
  • IBM i PTF Guide, Volume 28, Number 40

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