Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-05-14
4
Medium Priority
?
2,297 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 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

610 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