Solved

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

Posted on 2009-05-14
4
2,288 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 250 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
 

Author Comment

by:cheeseman-support
ID: 24423576
0

Featured Post

Technology Partners: 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

Read about achieving the basic levels of HRIS security in the workplace.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

730 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