Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Referencing field from a nested subselect in DB2 v8.1 fp14

Posted on 2009-05-14
4
Medium Priority
?
2,301 Views
Last Modified: 2012-05-07
I have a SELECT statement that has a nested select with a subselect, and I cannot reference a field from the top level select in the sub select.

I get an SQL0204N "TLO.DETAIL_LINE_ID" is an undefined name. SQLSTATE=42704 on the  "WHERE it.detail_line_id = tlo.detail_line_id" line of the subselect.  If I hardcode the detail_line_id, the query works perfectly.



The purpose of having the nested + subselect is to return the second row of the result, which will have a varying number of rows.  Basically I'm trying to emulating MSSQL's  "LIMIT 1,1" clause.

So the question is two part:
A) Is there a way to reference a field from the outer most SELECT in this example
B) Would it be easier to do something like FETCH ABSOLUTE?  How can I set up the cursor within this single query?
SELECT
	bill_number,
	(
		SELECT
			fit.trip_number
		FROM
			(
				SELECT
					o.changed, it.trip_number
				FROM
					itriptlo it
						INNER JOIN odrstat o ON it.detail_line_id = o.order_id AND it.trip_number = o.trip_number AND o.status_code = 'ASSGN'
				WHERE it.detail_line_id = tlo.detail_line_id
				ORDER BY changed ASC
				FETCH FIRST 2 ROWS ONLY
			) as FIT
		ORDER BY changed DESC
		FETCH FIRST ROW ONLY
	) as FirstIntermediateTrip
FROM
	tlorder tlo
WHERE
	Date(pick_up_by) BETWEEN '2009-03-01' AND '2009-03-01'
WITH UR

Open in new window

0
Comment
Question by:cheeseman-support
  • 3
4 Comments
 
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 1000 total points
ID: 24389028
Hi Cheesman,

References move outward.  You can reference the inner query items that are passed outward, but not vice-versa (as this query tries to do).

It may be easier to just join the tables and filter on the results instead of using the subselect.  And it may be easier to move the *detail_line_id* filter to the outermost query.


Good Luck,
Kent
0
 

Author Comment

by:cheeseman-support
ID: 24422404
I've tried moving the detail_line_id filter to the outside query, but when I do it kills the query performance by almost 15 seconds per row.
0
 

Accepted Solution

by:
cheeseman-support earned 0 total points
ID: 24423551
Solved:  used a table function and referenced that


CREATE FUNCTION tmw2tcg_trip_ord (input_order_id int)
RETURNS TABLE (trip_number int, trip_seq int)
LANGUAGE SQL READS SQL DATA NO EXTERNAL ACTION DETERMINISTIC
RETURN SELECT it.trip_number, Row_Number() Over(ORDER BY o.changed ASC) as trip_seq FROM itriptlo it INNER JOIN odrstat o ON it.detail_line_id = o.order_id AND it.trip_number = o.trip_number AND o.status_code = 'ASSGN' WHERE o.order_id = input_order_id;

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Among the most obnoxious of Exchange errors is error 1216 – Attached Database Mismatch error of the Jet Database Engine. When faced with this error, users may have to suffer from mailbox inaccessibility and in worst situations, permanent data loss.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question