Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

easy SQL question (but not for me)

Status
Not open for further replies.

slechols

Programmer
Nov 28, 2001
28
0
0
US
Can you reference a calculated field in the same view that it was calculated?

SELECT
g.niin,
CASE
when (e.total_needs + (m.mdr*6)) is null THEN
g.FUD
ELSE
(g.FUD - (e.total_needs + m.mdr*6))
END CASE
FROM
G072_VIEW g,
EXPRESS e,
MDR_VIEW m
WHERE
g.niin = e.niin AND g.niin = m.niin
 
CREATE OR REPLACE VIEW EXCESS_VIEW (NIIN,EXCESS)
AS
SELECT
g.niin,
CASE
when (e.total_needs + (m.mdr*6)) is null THEN
g.FUD
ELSE
(g.FUD - (e.total_needs + m.mdr*6))
END CASE
FROM
G072_VIEW g,
EXPRESS e,
MDR_VIEW m
WHERE
g.niin = e.niin AND g.niin = m.niin

I need to use EXCESS to calculate other fields.
 
How about simplifying this to:

SELECT
g.niin,g.FUD - NVL((e.total_needs + m.mdr*6),0)
FROM
G072_VIEW g,
EXPRESS e,
MDR_VIEW m
WHERE
g.niin = e.niin AND g.niin = m.niin;
 
Can I reference "EXCESS"(g.FUD-NVL((e.total_needs +m.mdr*6),0)) as "EXCESS" anywhere else in this query? I need to reference this number and another calculated number to derive 23 other fields.
 
If you alias the column, you can reference it in your ORDER BY clause, but that's about it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top