• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Partitioning Result Sets Using SQL

    November 27, 2023 Mike Larsen

    While working on a project recently, I needed to retrieve attributes of an item. While that sounds like a simple task, there was a twist. While reviewing the contents of the item master table, I noticed there could be multiple rows for the same item and each row was active and valid from a business perspective.

    I won’t get into the business end of it, but my goal was to return the most recent iteration of the item for further processing in the program. After doing some research and trying some potential solutions, I came across the SQL partition by clause and wanted to see if it could work for me.

    It makes sense, at this point, to show what the items look like (Figure 1).

    Figure 1: Item master data

    In this example, I have an item number (aka NDC_NUMBER), a GTIN that represents a case of the item, the case quantity, and a sequence number. When this item was originally created, it had a case GTIN identifier of 40300931135566 and the case held 48 items. At some point the manufacturer of the item changed the case quantity to 72 and assigned a new GTIN identifier to it. For the purposes of my program, I needed to return the most recent iteration of the item.

    After working with the partition by clause for a bit, I was able to write a statement that gave me exactly what I wanted (Figure 2). I’ll show the entire SQL statement, then I’ll break it down and explain the code.

    Figure 2: SQL statement with partition by

    On line 1, I’m building a common table expression to hold my result set. Lines 3 – 7 is where the magic happens. On line 3, I select the NDC, case GTIN, and case quantity from my item master table. The Row_number() function, on line 4, assigns a sequential number to each row in the result set. I’ll use that later. The over clause defines the result set on which I will perform an OLAP (Online analytical expression) operation. Next, the partition by clause is used to divide the result set into partitions. Partitioning a table makes it easier to manage and query the data and can improve query performance. I added an order by clause to sort the results in descending order. I did that because I want to get the most recent iteration of the item.

    In figure 3 and 4, I show the results of the SQL statement.

    Figure 3: SQL statement to show result set

     

    Figure 4. Result set

    I see that I have the two rows for the item, and I have the most recent version of the item in the first row. My final step is to add criteria to the SQL statement, so it only returns the first row (Figure 5).

    Figure 5. Final result set query

    When I run this query, I get just the row in which I’m interested (Figure 6).

    Figure 6. Final result set

    There are usually many options that solve a challenge when building a solution. This is the method I chose to solve mine and it is working perfectly.

    Mike Larsen is a director of information technology at Auburn Pharmaceutical and has been working with IBM i systems for over 20 years. He specializes in RPG, CL, and SQL and recently has been working with PHP and Python. Current projects have given Mike the opportunity to work with generating and parsing XML and JSON from SQL and consuming SOAP and REST web services. Although his main area of expertise is on IBM i, Mike has a passion for learning other languages and how he can integrate other platforms with IBM i.

    RELATED STORIES

    Guru: Comparing IFS Directories Using SQL

    Guru: String Manipulation Using SQL

    Guru: Regular Expressions, Part 1

    Guru: Regular Expressions, Part 2

    Guru: Debugging SQL Stored Procedures With ACS

    Guru: Creating PDF Documents With Python

    Guru: Creating Excel Spreadsheets With Python

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, FHG, Four Hundred Guru, IBM i, SQL

    Sponsored by
    Maxava

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    As I See It: Elusive Connections CloudSAFE And Focal Point Solutions Group Combine Services, Unify Brands

    4 thoughts on “Guru: Partitioning Result Sets Using SQL”

    • Rusty Gadberry says:
      November 27, 2023 at 11:38 am

      This should also give you the same result. Performance wise I don’t know which would be the best.
      SELECT a.NDC_NUMBER, b.GTIN_CASE, b.GTIN_CQTY, b.GTIN_SEQ
      FROM olsdta.GtinMasterTable a
      JOIN TABLE( SELECT GTIN_CASE, GTIN_CQTY, GTIN_SEQ
      FROM olsdta.GtinMasterTable b
      WHERE b.NDC_NUMBER = a.NDC_NUMBER
      ORDER BY GTIN_SEQ desc
      LIMIT 1 ) AS X ON 1=1
      WHERE trim(a.NDC_NUMBER) = ‘00093113556’;

      Reply
    • ema tissani says:
      November 27, 2023 at 12:38 pm

      If the case is using RPG in business logic, a simple CHAIN can be a very fast, idiomatic and terse solution for this kind of use cases, much less operations.
      Personally, I would avoid “trim” function in a key field to go easy on the SQL engine as much as possibile (of course if this is a program execute many times).

      Reply
    • roger nassar says:
      November 27, 2023 at 1:49 pm

      Hi Mike,
      Thank you for the continuous SQL info.
      As you said, many options are available to solve a challenge.
      Here is a simple alternate:
      If the table is not reorganized by key during its life, you can obtain the same result with the – relatively simple – following SQL:
      SELECT Ndc_number, Gtin_case, Gtin_Cqty FROM GtinMasterTable WHERE trim(ndc_number)=’xyz’ order by rrn(GtinMasterTable) desc LIMIT 1;

      I am assuming that if you had a “log” date in the table you would have ordered by it desc & gotten the same result as well.
      Thanks again for the info you continue to provide

      Reply
    • Testy MacTesterson says:
      May 14, 2025 at 8:04 am

      My f1st preference is to use the “IN” clause seems to me to be clearer in the intent:
      SELECT t1.NDC_NUMBER, t1.GTIN_CASE, t1.GTIN_CQTY, t1.GTIN_SEQ
      FROM olsdta.GtinMasterTable t1
      WHERE (t1.NDC_NUMBER,t1.GTIN_SEQ) IN (
      SELECT NDC_NUMBER, MAX(GTIN_SEQ) as max_GTIN_SEQ
      FROM olsdta.GtinMasterTable
      GROUP BY NDC_NUMBER
      )

      The 2nd is “JOIN”:
      SELECT t1.NDC_NUMBER, t1.GTIN_CASE, t1.GTIN_CQTY, t1.GTIN_SEQ
      FROM olsdta.GtinMasterTable t1
      JOIN ( SELECT NDC_NUMBER, MAX(GTIN_SEQ) as max_GTIN_SEQ
      FROM olsdta.GtinMasterTable
      GROUP BY NDC_NUMBER
      ) t2 on (t1.NDC_NUMBER,t1.GTIN_SEQ)=(t2.NDC_NUMBER,t2.max_GTIN_SEQ)

      Reply

    Leave a Reply Cancel reply

TFH Volume: 33 Issue: 73

This Issue Sponsored By

  • ProData
  • Focal Point Solutions Group
  • Briteskies
  • Raz-Lee Security
  • WorksRight Software

Table of Contents

  • IBM i Development Is Getting A Fresche Start With Some Ground-Breaking Subscriptions
  • CloudSAFE And Focal Point Solutions Group Combine Services, Unify Brands
  • Guru: Partitioning Result Sets Using SQL
  • As I See It: Elusive Connections
  • IBM i PTF Guide, Volume 25, Number 47

Content archive

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

Recent Posts

  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23
  • SEU’s Fate, An IBM i V8, And The Odds Of A Power13
  • Tandberg Bankruptcy Leaves A Hole In IBM Power Storage
  • RPG Code Generation And The Agentic Future Of IBM i
  • A Bunch Of IBM i-Power Systems Things To Be Aware Of
  • IBM i PTF Guide, Volume 27, Numbers 21 And 22

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