Solved

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

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

911 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

24 Experts available now in Live!

Get 1:1 Help Now