• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • SQL Finds a Delta

    December 7, 2011 Hey, Ted

    Is there a way using SQL to calculate the amount a numeric column changes from one row to the next?

    –Ken

    Yes. Here’s how it’s done.

    First, you need a table to use for an example.

    create table mydata
     ( Name char(8), Qty dec(3,0))
    
    insert into mydata values
      ('Bob',   25),
      ('Bill',  85),
      ('Sue',   17),
      ('Bob',   28),
      ('Bob',   32),
      ('Bill', 105),
      ('Sue',   45)
    

    To pull this off, the data must be stored in the order in which it is to be retrieved. If necessary, create a temporary table to hold the sorted data.

    declare global temporary table SortedData as
      (select name, qty
         from mydata
        order by name, qty)
    with data
    

    Join each row to the one before it.

    with NumberedData as
       (select rrn(x) as seq, x.name, x.qty
          from session/SortedData as x)
    select after.name,
           coalesce(before.qty,0) as before,
           after.qty as after,
           after.qty - coalesce(before.qty,0) as change
      from NumberedData as after
      left join NumberedData as before
        on after.name = before.name
       and after.seq = before.seq + 1
    

    NumberedData is a common table expression. It creates a temporary table in memory, with three columns: relative record number, name, and quantity. You have to have relative record number in order to join one row of NumberedData to another row of NumberedData. If you could see NumberedData, it would look like this:

    SEQ   NAME      QTY
      1   Bill       85
      2   Bill      105
      3   Bob        25
      4   Bob        28
      5   Bob        32
      6   Sue        17
      7   Sue        45
    

    In this example, I’m tracking the change for each person. I want to see the change between records 1 and 2, but not between 2 and 3. For that reason, the first part of the join compares the name columns of two rows to be sure they match. The second part of the join checks to see that the “before” row is numbered one less than the “after” row.

    Voilà!

    NAME    BEFORE   AFTER  CHANGE
    Bill         0     85       85
    Bill        85    105       20
    Bob          0     25       25
    Bob         25     28        3
    Bob         28     32        4
    Sue          0     17       17
    Sue         17     45       28
    

    –Ted



                         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
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Micro Focus:  RUMBA for iSeries, the world's most used terminal emulation software
    The 400 School:  Fall Training Sale – Discounts up to 40%! RPG IV COBOL CL Admin Security
    Infor:  Increase performance and profitability with Infor10 Distribution and IBM Power Systems

    IT Jungle Store Top Book Picks

    BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

    The iSeries Express Web Implementer's Guide: List Price, $49.95
    The iSeries Pocket Database Guide: List Price, $59
    The iSeries Pocket SQL Guide: List Price, $59
    The iSeries Pocket WebFacing Primer: List Price, $39
    Migrating to WebSphere Express for iSeries: List Price, $49
    Getting Started with WebSphere Express for iSeries: List Price, $49
    The All-Everything Operating System: List Price, $35
    The Best Joomla! Tutorial Ever!: List Price, $19.95

    Blade Servers Barely Nick IBM i Market Get With The Program

    Leave a Reply Cancel reply

Volume 11, Number 37 -- December 7, 2011
THIS ISSUE SPONSORED BY:

WorksRight Software
SEQUEL Software
Shield Advanced Solutions

Table of Contents

  • Juggling With jQuery
  • SQL Finds a Delta
  • Admin Alert: More Information on Semi-Restricted State, Vendor Profiles, and Storage Pools

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