473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Scope problem? UDB DB2 R8.1.5 Linux

Apparently I have a scope problem wrt t1.start_date in the second sub
select's sub select. The message is:

S0002--[IBM][CLI Driver][DB2/LINUX] SQL0204N "T1.START_DATE" is an
undefined name. SQLSTATE=42704

The query is:

SELECT t1.*,
start_date + (heat_day-1) days AS heat_date_proj,
start_date + (flush_day-1) days AS recov_date_proj,
t3.datex
FROM $schema.flushes t1
JOIN $schema.flush_protocols t2 ON t1.protocol_id=t2.protocol_i
AND t1.controller=t2.controller
LEFT OUTER JOIN
( SELECT * FROM $schema.heat_obs
WHERE t1.donor_bhid=bhid
AND datex =
( SELECT max(datex) FROM $schema.heat_obs
WHERE t1.donor_bhid=bhid
AND datex BETWEEN t1.start_date
AND t1.start_date + 18 days
)
) t3 ON t1.donor_bhid=t3.bhid
WHERE donor_bhid=$bhid
AND start_date='$start_date'";

I thought (obviously erroneously) that the data from any enclosing
select was referencable within a sub select. What is the correct form
for this? Something involving WITH?
Nov 12 '05 #1
6 2051
I think this is closer to what you want. Since you only wanted one
column back from t3 and you were willing to accept a NULL datex (as
evidenced by the LEFT OUTER JOIN in your query), you can get the
desired result from a nested SELECT:

SELECT
t1.*
, start_date + (heat_day - 1) DAYS AS heat_date_proj
, start_date + (flush_day - 1) DAYS AS recov_date_proj
, (SELECT MAX(datex) FROM $schema.heat_obs t3
WHERE t3.bhid = t1.donor_bhid
AND t3.datex BETWEEN t1.start_date AND
t1.start_date + 18 DAYS
) AS datex
FROM $schema.flushes t1
INNER JOIN $schema.flush_protocols t2
ON t1.protocol_id = t2.protocol_id
AND t1.controller = t2.controller
WHERE t1.donor_bhid = $bhid
AND t1.start_date = '$start_date'
;

There may be syntax errors, but I hope this is of some help to you.

Fred Sobotka
FRS Consulting, Inc
http://www.frsconsulting.com

Nov 12 '05 #2
"TABLE" keyword will resolve your problem.

SELECT t1.*,
start_date + (heat_day-1) days AS heat_date_proj,
start_date + (flush_day-1) days AS recov_date_proj,
t3.datex
FROM $schema.flushes t1
JOIN $schema.flush_protocols t2 ON t1.protocol_id=t2.protocol_id
AND t1.controller=t2.controller
LEFT OUTER JOIN
TABLE
( SELECT * FROM $schema.heat_obs
WHERE t1.donor_bhid=bhid
AND datex =
( SELECT max(datex) FROM $schema.heat_obs
WHERE t1.donor_bhid=bhid
AND datex BETWEEN t1.start_date
AND t1.start_date + 18 days
)
) t3 ON t1.donor_bhid=t3.bhid
WHERE donor_bhid=$bhid
AND start_date='$start_date'";

Nov 12 '05 #3
Tonkuma wrote:
"TABLE" keyword will resolve your problem.

SELECT t1.*,
start_date + (heat_day-1) days AS heat_date_proj,
start_date + (flush_day-1) days AS recov_date_proj,
t3.datex
FROM $schema.flushes t1
JOIN $schema.flush_protocols t2 ON t1.protocol_id=t2.protocol_id
AND t1.controller=t2.controller
LEFT OUTER JOIN
TABLE
( SELECT * FROM $schema.heat_obs
WHERE t1.donor_bhid=bhid
AND datex =
( SELECT max(datex) FROM $schema.heat_obs
WHERE t1.donor_bhid=bhid
AND datex BETWEEN t1.start_date
AND t1.start_date + 18 days
)
) t3 ON t1.donor_bhid=t3.bhid
WHERE donor_bhid=$bhid
AND start_date='$start_date'";

Thank you. That is just what I needed.

Is there some documentation of this keyword (as opposed to the function
by the same name) other than the one or two "must" references in Vol. 1
of the SQL Reference Manual?
Nov 12 '05 #4
fr**********@gmail.com wrote:
I think this is closer to what you want. Since you only wanted one
column back from t3 and you were willing to accept a NULL datex (as
evidenced by the LEFT OUTER JOIN in your query), you can get the
desired result from a nested SELECT:

SELECT
t1.*
, start_date + (heat_day - 1) DAYS AS heat_date_proj
, start_date + (flush_day - 1) DAYS AS recov_date_proj
, (SELECT MAX(datex) FROM $schema.heat_obs t3
WHERE t3.bhid = t1.donor_bhid
AND t3.datex BETWEEN t1.start_date AND
t1.start_date + 18 DAYS
) AS datex
FROM $schema.flushes t1
INNER JOIN $schema.flush_protocols t2
ON t1.protocol_id = t2.protocol_id
AND t1.controller = t2.controller
WHERE t1.donor_bhid = $bhid
AND t1.start_date = '$start_date'
;

There may be syntax errors, but I hope this is of some help to you.

Fred Sobotka
FRS Consulting, Inc
http://www.frsconsulting.com

That would work, but this is just my most simple example; I usually want
multiple columns from the selected row, if there is one. A later poster
(Tonkuma) pointed me to a more general solution, the keyword TABLE.
Nov 12 '05 #5
I don't know other documentation than SQL Reference.
SQL Reference Volume 1 ---> Chapter4. Queries ---> Subselect --->
Table-reference ---> Correlated reference in Table-reference

Nov 12 '05 #6
Tonkuma wrote:
I don't know other documentation than SQL Reference.
SQL Reference Volume 1 ---> Chapter4. Queries ---> Subselect --->
Table-reference ---> Correlated reference in Table-reference

The SQL standard keyword is LATERAL. It is tolerated in DB2 UDB V8.2.
Googling for SQL LATERAL yields a bit more information.

Here is a longer blurb on on the lore of TABLE and LATERAL as it has
been handed down to me:
http://www.webservertalk.com/archive...-6-268771.html

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
2
by: Wat | last post by:
If a class is not given a namespace, what does this imply? From inside of the class, if to call global functions, should global scope :: be used at all? If global scope :: used, what benefits...
4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
1
by: Robert North | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've just started programming JS, and one question keeps nagging me: I can insert a <script> element anywhere in HTML, and when it's executed...
165
by: Dieter | last post by:
Hi. In the snippet of code below, I'm trying to understand why when the struct dirent ** namelist is declared with "file" scope, I don't have a problem freeing the allocated memory. But...
14
by: Darren L. Weber | last post by:
I am trying to compile a utility to create .avi files. See http://cpbotha.net/im2avi I'm working on Debian etch (a mix of testing/unstable). dweber@dnlweber:~/im2avi-0.4$ g++ --version g++...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
6
by: finer recliner | last post by:
i've been playing with the below code for a while now, and i cant get it to work correctly. the user should be able to input text into a textbox and upon hitting submit, it will append that text to...
3
by: psroga | last post by:
Can anyone look at this code and let me know why pthread_mutex_unlock and pthread_mutex_lock are giving me the "phtread_mutex_unlock" was not defined in this scope error. I am compiling the code...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.