Solved

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

Posted on 2009-05-14
4
2,266 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 45

Assisted Solution

by:Kdo
Kdo earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:cheeseman-support
Comment Utility
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now