Newsletters   Subscriptions  Forums  Store  Media Kit  About Us  Contact  Search   Home 
fhg
Volume 4, Number 10 -- March 31, 2004

Thinking in Sets


Hey, Howard:

I have an interesting problem and was wondering if you could show me how to code it in SQL. I have a file that records changes to packages in our facility. The file has a package ID number, date of activity, time of activity, and activity code. I want to find the last date and time of activity for each package but can't get the query to work, because using MAX on the date and time can return a time value for when an activity did not occur.

--Michael


This is a really interesting problem, Michael, because your schema does not use the timestamp data type, but rather two separate fields--a date and a time--to record the activity. However, thinking in sets and relating the sets together can solve this problem.

First, I've created a small sample table to insert data for test queries:

CREATE TABLE SQLBOOK.T1AA
	(ID INTEGER NOT NULL,
	 ACTDATE	DATE NOT NULL,
	 ACTTIME 	TIME NOT NULL);
	
INSERT INTO SQLBOOK.T1AA VALUES (1,'01/01/2003','01:00');	
INSERT INTO SQLBOOK.T1AA VALUES (1,'01/02/2003','02:00');	
INSERT INTO SQLBOOK.T1AA VALUES (1,'01/03/2003','03:00');	
INSERT INTO SQLBOOK.T1AA VALUES (2,'01/01/2003','02:00');	
INSERT INTO SQLBOOK.T1AA VALUES (3,'01/02/2003','03:00');	
INSERT INTO SQLBOOK.T1AA VALUES (3,'01/03/2003','02:00');	
INSERT INTO SQLBOOK.T1AA VALUES (3,'01/04/2003','01:00');

If you query the table you should get results like the following:


ID

ACTDATE

ACTTIME

1

01/01/2003

1:00:00 AM

1

01/02/2003

2:00:00 AM

1

01/03/2003

3:00:00 AM

2

01/01/2003

2:00:00 AM

3

01/02/2003

3:00:00 AM

3

01/03/2003

2:00:00 AM

3

01/04/2003

1:00:00 AM


Now, one would assume that you could simply code a query using max and achieve the desired results, but doing that query leads to data that is not in the result set. Here is an example:

SELECT ID, MAX(ACTDATE) AS D, MAX(ACTTIME) AS T
FROM SQLBOOK.T1AA
GROUP BY ID;

That query would result in the following:


ID

D

T

1

01/03/2003

3:00:00 AM

2

01/01/2003

2:00:00 AM

3

01/04/2003

3:00:00 AM


What's wrong with the above result set? Notice that result for ID 3 says that the last activity occurred on 1/4/2003 at 3:00 AM; however, if you look at the source data, the activity on 1/4/2003 occurred at 1:00 AM. By using MAX, you are creating a result set of incorrect data. Now, consider the following query:

SELECT ID, MAX(ACTDATE) AS D
FROM SQLBOOK.T1AA
GROUP BY ID;

This query returns a set I will call X:


ID

D

1

01/03/2003

2

01/01/2003

3

01/04/2003


The above query returns a result set that correctly identifies the date of each last activity. What you need to do is join that result set back to the table to retrieve the time of the last activity on the given date. Here is a query that returns the last time of an activity for a given ID on each date:

SELECT ID, ACTDATE, MAX(ACTTIME) AS T
FROM SQLBOOK.T1AA
GROUP BY ID, ACTDATE;

This query returns a set I will call Y:


ID

ACTDATE

T

1

01/01/2003

1:00:00 AM

1

01/02/2003

2:00:00 AM

1

01/03/2003

3:00:00 AM

2

01/01/2003

2:00:00 AM

3

01/02/2003

3:00:00 AM

3

01/03/2003

2:00:00 AM

3

01/04/2003

1:00:00 AM


Why did I elect to use MAX in returning the time? Just in case there are several entries for the same day. Remember, I want the last time that something happened to a given ID on a date. If I have more than one record for a single ID on a given date, without using MAX and a group by the query would return two records. Now that I have two sets of data (X and Y in the above examples), I can simply join them together to find the date and time of the last activity:

SELECT X.ID, D, T
FROM (SELECT ID, MAX(ACTDATE) AS D
		FROM SQLBOOK.T1AA
		GROUP BY ID) AS X
	INNER JOIN 
		(SELECT ID, ACTDATE, MAX(ACTTIME) AS T
		 FROM SQLBOOK.T1AA
		GROUP BY ID, ACTDATE) AS Y
	ON (X.ID=Y.ID AND X.D = Y.ACTDATE);

Results in this set:


ID

D

T

1

01/03/2003

3:00:00 AM

2

01/01/2003

2:00:00 AM

3

01/04/2003

1:00:00 AM


See, the above query uses an inner join to join the set X to the set Y, where the ID codes are equal and the dates are equal. In this manner, I get a set of information showing the last date and time of an activity against an ID code.

Remember, a table is just a set of data. An SQL statement is a set of data. SQL allows you to join sets of data together. If you think in sets, relational gets a whole lots easier and opens up a lot of possibilities in your queries and reporting. Besides, it's fun.


Howard F. Arner, Jr., is a writer and consultant with Client Server Development and is the author of the book iSeries and AS/400 SQL at Work. Howard also designed SQLThing Enterprise Edition, a query program and stored procedure editor specifically for the AS/400's unique capabilities. You can purchase a copy of Howard's book or learn more about SQLThing or SQL on the iSeries at www.sqlthing.com. Send your SQL questions to Howard at harner@sqlthing.com.

Sponsored By
GST

Mirrored LTO-2 Backup

GST, Inc.
Phone: 866-478-4621 or 949-900-1090 (for sales x 300)
Web: http://www.gstinc.com
Email: sales@gstinc.com

---------------------------------------------------------------------

SUMMARY

High-performance end of GST's dual-drive subsystems offers fault tolerant mirrored backups plus off-line copy and cascading for LTO.

· Dual LTO-2 drives.
· Capacity to 800GB
· Speed up to 252GB/hour
· Mirrored Backup supports DR.
· LVD, HVD and Fibre connectivity.
· Starts at $14,500 USD.

---------------------------------------------------------------------

THE BIG PICTURE

· Dual LTO-1 or LTO-2 tape drives in external enclosure.
· Capacity: LTO-2 is 200GB native (400GB compresed).
· Speed: LTO-2 is 126GB/hour native (252GB/hr compressed).
· Mirrored backup provides disaster recovery support.
· Added functions: off-line copy plus cascading.
· LCD operator panel improves backup efficiency.
· 2-level media protection with LTO-1 and LTO-2.
· Roadmap to LTO-3 and LTO-4.
· LTO range: single drive to enterprise-wide libraries.
· Supports LVD, HVD, and FC connections.

Our Dual LTO Tape Subsystem is part of GST's SafeDR™ family. Dual drives generate identical sets of backup cartridges so that one set can be safeguarded remotely for disaster recovery while the other set is retained onsite for faster restores; other tape functions add off-line copy and cascading capabilities. An LCD panel provides an advanced operator interface for visibility over backup operations.

The Dual LTO Tape Subsystem can backup a wide variety of servers. They connect natively with LVD (low voltage differential), HVD (high voltage differential), and FC (Fibre Channel) interfaces.

MODEL OVERVIEW

GST's Dual LTO Tape Subsystem is available in a variety of models. Each LTO technology (LTO-1, LTO-2) is available in two server-to-SCSI Adapter interfaces and one fibre interface:

LOW VOLTAGE DIFFERENTIAL (LVD). Maximum SCSI negotiation of 160MB/s; the total server-to-storage peripheral connectivity is 12 meters.

HIGH VOLTAGE DIFFERENTIAL (HVD). Maximum SCSI negotiation of 40MB/s; the total server-to-storage peripheral connectivity is 25 meters.

FIBRE CHANNEL (FC). 2Gbit fibre interface.

LTO TECHNOLOGY

LTO tape technologies supported are LTO-1 and the newer LTO-2. Native capacities for LTO-1 and LTO-2 are 100GB and 200GB (200GB, 400GB compressed) with speeds of 54GB/hour and 126GB/hour (108GB/hour, 252GB/hour compressed). A 4-generation roadmap provides for a native capacity of 800GB per cartridge (1.6TB compressed) and a transfer rate of up to 576GB/hour (1,152GB/hour compressed).

Cartridges use linear (serpentine) tape format providing fewer moving parts, durability and reduced maintenance costs. The version of LTO technology that GST uses is best suited for high-capacity backup, restore and archiving. Enhancements in the areas of timing-based servos, hardware data compression, optimized track layouts and ECC error correction help improve capacities, performance and reliability.

KEY BENEFITS

IMPROVED DR - Mirrored backup capability produces simultaneous sets of identical backup cartridges with no performance penalty; this permits storage of one set off-site for better disaster recovery and storage of the other set on-site for rapid restore.

FAULT-TOLERANT BACKUP AND RESTORE - Dual-drive configurations ensure against drive failure since the second drive continues to operate, ensuring the backup is completed successfully.

ADDED TAPE FUNCTIONS - For dual-drive subsystems, key added tape functions include: dual-drive mirroring for mirrored backups, off-line copying/verifying with no processor penalty, and cascading to extend unattended backup capacity by switching from one drive to the other (doubling capacity).

SPACE SAVER - The small dimensions and light weight of the configuration facilitate the optimum use of datacenter space.

MEDIA INVESTMENT PROTECTION - For IT sites with an investment in LTO-1 media, the newer LTO-2 technology is backward-read compatible, and forward-read compatible with future generations.

KEY FEATURES

DUAL DRIVE CONTROLLER - Manages mirroring, off-line copying, cascading, and the LCD Display Panel.

LCD DISPLAY PANEL - Multi-function LCD Display Panel continuously displays key drive and backup job conditions.

CONVENIENT DRIVE INTERFACE - RS232 interface for updating firmware and optimizing drive performance.

SERVICE AND SUPPORT - A variety of support programs provide installation and onsite service; includes GST's Accelerated Ship Program (GASP) to get replacement units on the scene rapidly.

WARRANTY - Purchase price includes a built-in manufacturer's warranty of three years.

GST, Inc.
Phone: 866-478-4621 or 949-900-1090 (for sales x 300)
Web: http://www.gstinc.com
Email: sales@gstinc.com


Editors: Howard Arner, Joe Hertvik, Ted Holt,
Shannon O'Donnell, Kevin Vandever
Managing Editor: Shannon Pastore
Contributing Editors: Raymond Everhart, G. Wayne Hawks,
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.

THIS ISSUE
SPONSORED BY:

Guild Companies
Client Server Development
GST
SuSE Linux
COMMON


BACK ISSUES

TABLE OF
CONTENTS
Step by Step: RPG IV and Interactive Web Pages

Determine Elapsed Days, Weekdays, and Workdays

Thinking in Sets

Admin Alert: Moving ASCII Data Between IFS and Windows

OS/400 Alert: Virus Programming for the Novice



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