RE: calculating values to be used later in Oracle SQL

From: Mark W. Farnham <mwf_at_rsiz.com>
Date: Fri, 21 Feb 2014 12:33:49 -0500
Message-ID: <0c7a01cf2f2b$15974c90$40c5e5b0$_at_rsiz.com>



+1, but understand this is a sqlplus feature and I’m not sure what interface you’re using.  

I’m also unclear whether you need a scalar value or a set of dates for a set of patients.  

mwf  

From: oracle-l-bounce_at_freelists.org [mailto:oracle-l-bounce_at_freelists.org] On Behalf Of David Fitzjarrell Sent: Friday, February 21, 2014 12:17 PM To: William.Threlfall_at_albertahealthservices.ca; Oracle-L_at_freelists.org Subject: Re: calculating values to be used later in Oracle SQL  

The new_value parameter to the column directive should help you:

column lasttx new_value last_tx

Now you can use &last_tx throughout the transaction or script:

...
where x.date_of_lab > &last_tx
...    

David Fitzjarrell
Primary author, "Oracle Exadata Survival Guide"  

On Friday, February 21, 2014 10:11 AM, William Threlfall <William.Threlfall_at_albertahealthservices.ca> wrote:

Question for the SQL experts …

I need to calculate a value and then use it many times as a criterion for other selections. What is the best way to do that in Oracle SQL?

For example, I need to calculate, for each patient, the date of last transplant (call it LASTTX) and then select a bunch of lab results where one of the criteria in the WHERE clause would be DATE_OF_LAB > LASTTX.  

In the following example I want to select the greatest non-NULL CREAT lab result from the most recent DATE_OF_LAB where the DATE_OF_LAB is greater than the date of last transplant.  

This subquery gives me the date of last transplant:  

(TO_DATE((MAX(PT.DATE_OF_TRANS) OVER (PARTITION BY PT.PAT_ID ORDER BY PT.PAT_ID
    ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)), 'yyyy.mm.dd')) AS LASTTX,  

However, if I try to use LASTTX in subsequent SELECT subquery statements, I get an error that it is an invalid identifier.  

Is there a way to do what I want without having to use that code snippet dozens of times, as in:  

(SELECT MAX(CREAT) KEEP (DENSE_RANK FIRST ORDER BY X.DATE_OF_LAB DESC, X.CREAT DESC)
    FROM PATIENT_LABS X     WHERE X.CREAT IS NOT NULL     AND X.PAT_ID = PD.PAT_ID     AND X.DATE_OF_LAB > LASTTX) AS MRCREAT   The nuisance way would be to re-use that code snippet over and over again, such as:  

(SELECT MAX(CREAT) KEEP (DENSE_RANK FIRST ORDER BY X.DATE_OF_LAB DESC, X.CREAT DESC)
    FROM PATIENT_LABS X     WHERE X.CREAT IS NOT NULL     AND X.PAT_ID = PD.PAT_ID     AND X.DATE_OF_LAB >        (SELECT MAX(PT.DATE_OF_TRANS) OVER (PARTITION BY PT.PAT_ID ORDER BY PT.PAT_ID         ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)         FROM PAT_TRANSPLANT PT)   ) AS MRCREAT   I think there ought to be a way to calculate variables and use them later on, but I’m just not clueing in at the moment.  

Thanks, - Bill.  


Willliam J. (Bill) Threlfall, MSc

Clinical Informatics Coordinator - OTTR

Transplant Services, University of Alberta Hospital

Alberta Health Services

Aberhart Centre, Room 9221

8440 112 Street

Edmonton, AB T6G 2B7  

Telephone: 780-407-6175 FAX: 780-407-8981  

Alberta Health Services

www.albertahealhservices.ca          


This message and any attached documents are only for the use of the intended recipient(s), are confidential and may contain privileged information. Any unauthorized review, use, retransmission, or other disclosure is strictly prohibited. If you have received this message in error, please notify the sender immediately, and then delete the original message. Thank you.  

--
http://www.freelists.org/webpage/oracle-l
Received on Fri Feb 21 2014 - 18:33:49 CET

Original text of this message