fhg
Volume 7, Number 29 -- August 8, 2007

Which One Is the Culprit?

Published: August 8, 2007

Hey, Ted:

I am using the MAX function in a SQL statement to get the maximum value from a few columns. I don't know which element is producing the maximum value. Is there a way to identify the element that has the maximum value?

--Hisham


Yes, Hisham. Thanks for allowing me another opportunity to show off that most versatile of SQL features: CASE.

For the benefit of the readers, here's the SQL statement Hisham sent to me.

SELECT max(WK01, WK02, WK03, WK04) FROM SALES

Here's the sort of results his SQL query generates.

 
MAX 
===
100 
120 
150

He has the maximum value from each input row (record), but how does he know which column (field) had the maximum value? Like this:

SELECT MAX(wk01, wk02, wk03, wk04), 
 CASE WHEN wk01 = MAX(wk01, wk02, wk03, wk04) THEN 1 
      WHEN wk02 = MAX(wk01, wk02, wk03, wk04) THEN 2 
      WHEN wk03 = MAX(wk01, wk02, wk03, wk04) THEN 3 
      WHEN wk04 = MAX(wk01, wk02, wk03, wk04) THEN 4 
 END AS MaxPos 
FROM SomeTable

In case of a tie, the CASE expression returns the position of the first one.

Suppose the input data looks like this:

Wk01   Wk02   Wk03   Wk04
====   ====   ====   ====
  25    100     75     80  
 120    100     55    110
 120    150    150     90

Now the results look like this:

 
MAX     MaxPos 
===     ======
100       2
120       1
150       2

CASE is a marvelous tool. I find new uses for it often, and we've presented several examples of the use of CASE in previous editions of Four Hundred Guru.

--Ted



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


Sponsored By
PRODATA COMPUTER SERVICES

Remote Database Access!

DBU Remote Database plug-in allows you to access
your MySQL, SQL, DB2 and Oracle databases from your System i !

Instantly display and edit these files
using DBU, database utility.

Then team it all up with DBU Audit
to capture the adds, changes and deletes
made to the data! It's that simple.

Contact us today at 800.228.6318 or
sales@prodatacomputer.com
for your FREE 30-day licensed copy.

www.prodatacomputer.com


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
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

DRV Technologies:  Automatically convert and distribute AS/400 reports with SpoolFlex
Maximum Availability:  The ultimate System i replication for business of all sizes
COMMON:  Join us at the Annual 2008 conference, March 30 - April 3, in Nashville, Tennessee


IT Jungle Store Top Book Picks

The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95

 

The Four Hundred
System i Announcement Wrap Up

IBM Upgrades System i Storage with SAS Drives

Mainframe Vendor BluePhoenix Ready to Purchase ASNA

As I See It: Policeware

The Linux Beacon
AMD Gooses Dual-Core Opteron Speeds, Cuts Prices

IBM Takes Its Own Server Consolidation Medicine

NEC Brings Linux, Windows Clustering Software to North America

As I See It: Policeware

Four Hundred Stuff
IBM Prepares to Launch DB2 Web Query for System i

IBM Details MySQL on System i Offering

CCSS Adds Predictive Capability to System Monitoring Tool

VAI Automates Service Calls, Tightens Inventory Control

Big Iron
SHARE to Emphasize Business Continuity at Upcoming Conference

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
August 4, 2007: Volume 9, Number 31

July 28, 2007: Volume 9, Number 30

July 21, 2007: Volume 9, Number 29

July 14, 2007: Volume 9, Number 28

July 7, 2007: Volume 9, Number 27

June 30, 2007: Volume 9, Number 26

The Windows Observer
Software Assurance is 'Healthy and Vibrant,' Microsoft Claims

Office Communicator 2007 Goes Gold

MojoPac Delivers Windows OS, Apps on a Thumb Drive

SteelEye Delivers CDP for Windows Servers

The Unix Guardian
Q&A: Sun's Top Operating System Brass Talk OS Strategy

Sun Exceeds Margin Goals in Q4 on Flat Sales

The IT Job Market Is More Competitive, Says Gartner

IBM Shows Off Web 2.0 Stuff with Lotus Quickr

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

THIS ISSUE SPONSORED BY:

Help/Systems
ProData Computer Services
Guild Companies



TABLE OF CONTENTS
Let WDSc Help You Format Your Source Code

Which One Is the Culprit?

Admin Alert: Eliminating Easy-to-Guess User Passwords

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Rexx STDIN File

What is holding companies back from using CASE tools?

Hexadecimal thumbprint of a file

Duplicated printer files

Problem with "cpyfrmimpf"





 
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-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement