fhg
Volume 10, Number 33 -- October 27, 2010

Let One Row Represent a Group, Take 2

Published: October 27, 2010

by Ted Holt

Let's return to a problem I wrote about a few months ago. The technique I illustrated in that article works for most implementations of SQL. Today it's my pleasure to share a technique you can use with versions of SQL that include certain OLAP functions, such as DB2 for i V5R4 or above.

You've been asked to list customer orders that have not been fully shipped, but you are to only show the first unshipped line of each order. Use the RANK() function to assign a sequential number to each row for each order, like this:

select OrderNo, LineNo, QtyOrdered - QtyShipped as Balance,
       substr(char(CrtTime), 1, 16) as TimeEntered,
       rank() over
          (partition by OrderNo order by CrtTime) as OrderRank
     from salesordd
    where QtyShipped < QtyOrdered
  • RANK() OVER means that SQL is to number the rows.
  • ORDER BY CrtTime means that the rows are to be numbered in sequence according to the CrtTime field.
  • PARTITION BY OrderNo means that the numbering is to restart at 1 for each order number. Without PARTITION BY, the entire result set would be numbered in one sequence.

This is the result set:

ORDERNO  LINENO   BALANCE   TIMEENTERED       ORDERRANK
    101      2         12   2010-04-21-11.15          1
    102      1          5   2010-04-21-10.05          1
    102      2        100   2010-04-21-10.15          2
    103      3          5   2010-04-21-14.37          1
    105      2          6   2010-04-21-10.30          1

Notice that there are two unshipped lines for order 102. In order to get only one line per order, you must select the rows with a rank value of 1. However, it is not permitted to refer to the ranking column in the WHERE clause. To get around this limitation, put the result set into something temporary, such as a common table expression:

with Temp as 
  (select d.*, 
          rank() over
            (partition by OrderNo order by CrtTime) as OrderRank
     from salesordd as d
    where QtyShipped < QtyOrdered)
    
select t.OrderNo, t.LineNo, t.QtyOrdered, t.QtyShipped,
       (t.QtyOrdered - t.QtyShipped) as Balance, t.Price,
       (t.QtyOrdered - t.QtyShipped) * t.Price as Extended,
       substr(char(t.crttime), 1, 16) as TimeEntered 
  from temp as t
 where t.OrderRank = 1

Or a derived table:

select t.OrderNo, t.LineNo, t.QtyOrdered, t.QtyShipped,
       (t.QtyOrdered - t.QtyShipped) as Balance, t.Price,
       (t.QtyOrdered - t.QtyShipped) * t.Price as Extended,
       substr(char(t.crttime), 1, 16) as TimeEntered
  from 
  (select d.*,
          rank() over
            (partition by OrderNo order by CrtTime) as OrderRank
     from salesordd as d
    where QtyShipped < QtyOrdered) as t
 where t.OrderRank = 1

Either way, here's the result set:

ORDER  LINE  ORDERED  SHIPPED   BALANCE   PRICE  EXTENDED  TIMEENTERED
  101    2        12        0        12    1.00     12.00  2010-04-21-11.15
  102    1        10        5         5    2.00     10.00  2010-04-21-10.05
  103    3         5        0         5    1.10      5.50  2010-04-21-14.37
  105    2         6        0         6    2.00     12.00  2010-04-21-10.30

RELATED STORIES

Let One Row Represent a Group

New in V5R4: OLAP Ranking Specifications



                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


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
E-mail: software@worksright.com
Web site: www.worksright.com


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Erwin Earley, Brian Kelly, Michael Sansoterra
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

PowerTech:  FREE Webinar! Protect IBM i Data from FTP, ODBC, & Remote Command. Nov. 3, 10 am CT
iSeries DevCon2010:  Get 3 days of IBM i training and 365 DAYS OF VALUE, Nov 15-17, Las Vegas
Bsafe Information Systems:  Webcast: Top 5 System i Vulnerabilities. Oct 27. 2 p.m. EST


 

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


 
The Four Hundred
IBM i Competes with AIX/Oracle on Power 720s, Gets Beat on 750s

iManifest Powers On in Japan

Power Systems Not Quite Rebounding Yet in Q3

As I See It: My Name is Bond--Jane Bond

Job Site Data Shows IT Jobs Down Across the Board

Four Hundred Stuff
IBM i 7.1 Supported by 120 ISVs with 300+ Apps, According to IBM Directory

Numina Voice Picking Boosts Productivity of Wisconsin Warehouse

HarrisData Continues to Modernize IBM i ERP Suite

Free RPG Editor Takes on Unit Testing

ExaGrid Makes No Compromises with 'DeltaZone' Backups

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
September 25, 2010: Volume 12, Number 39

September 18, 2010: Volume 12, Number 38

September 11, 2010: Volume 12, Number 37

September 4, 2010: Volume 12, Number 36

August 28, 2010: Volume 12, Number 35

August 21, 2010: Volume 12, Number 34

TPM at The Register
Teradata pumps data warehouses with six-core Xeons

NEC cranks fault-tolerant servers with Xeon six-shooters

Cray beats six to win Argonne deal

Citrix puffs profits 64% with cloudy wares

Cisco eats own cloud food

OpenStack unfurls first full cloud fluffer

Intel broaches fresh barrel-o-Linux

Red Hat exec proposes end to IT suckage

IBM kills off Irish server works

OpenSFS to fund Lustre HPC file system development

Juniper boasts billion buck quarter

Intel waves Old Glory with wafer baker plans

THIS ISSUE SPONSORED BY:

ProData Computer Services
WorksRight Software
inFORM Decisions


Printer Friendly Version


TABLE OF CONTENTS
DDS Design: The RD Power Way

Let One Row Represent a Group, Take 2

Printing Multiple PC5250 Screens at the Same Time

Four Hundred Guru

BACK ISSUES




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2010 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement