ACSDBA
asked on
Data Calculation of multiple rows
select cph.reoccupancy_phase,
itr.approved,
itr.public_housing+itr.tax _credit+it r.pbra+itr .market total_returned,
itr.next_phase
from pmco.communities com,
pmco.community_occ_phase cph,
(select m.hoh_project_no, count(m.hoh_ss_number) total_households
from pmco.hoh_master m
group by m.hoh_project_no) mas,
pmco.itr_eligible_to_retur n_v itr
where com.affected_rel_community = 'Yes'
and com.community_project_numb er = :PROJECT_NO
and com.community_project_numb er = cph.community_id
and com.community_project_numb er = mas.hoh_project_no
and com.community_project_numb er = itr.project_no
and itr.return_phase = cph.reoccupancy_phase
order by 1
occ_phase|eligible_to_retu rn|approve d|total_re turned|nex t_phase
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- --------
1 48 45 44
2 22 0 0
3 1 1 0
4 1 0 0
Experts,
I have the following query and the output from that query that I need to ask a question about. I want to calculate the last column(next phase). The logic for that calculation is this. Ex.. 45(from occ_phase1) - 44(also from occ_phase1) + 22(from occ_phase2) to calculate the next phase for occ_phase 1.
How in the world do I write that? I am totally stumped at this point. Thanks!
itr.approved,
itr.public_housing+itr.tax
itr.next_phase
from pmco.communities com,
pmco.community_occ_phase cph,
(select m.hoh_project_no, count(m.hoh_ss_number) total_households
from pmco.hoh_master m
group by m.hoh_project_no) mas,
pmco.itr_eligible_to_retur
where com.affected_rel_community
and com.community_project_numb
and com.community_project_numb
and com.community_project_numb
and com.community_project_numb
and itr.return_phase = cph.reoccupancy_phase
order by 1
occ_phase|eligible_to_retu
--------------------------
1 48 45 44
2 22 0 0
3 1 1 0
4 1 0 0
Experts,
I have the following query and the output from that query that I need to ask a question about. I want to calculate the last column(next phase). The logic for that calculation is this. Ex.. 45(from occ_phase1) - 44(also from occ_phase1) + 22(from occ_phase2) to calculate the next phase for occ_phase 1.
How in the world do I write that? I am totally stumped at this point. Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
should be:
select occ_phase, ..... , approved - total_returned+ lead(eligible_to_return) over (order by occ_phase) next_phase
from
(select occ_phase, eligible_to_return, approved, total_returned from XXXX)
/
select occ_phase, ..... , approved - total_returned+ lead(eligible_to_return) over (order by occ_phase) next_phase
from
(select occ_phase, eligible_to_return, approved, total_returned from XXXX)
/
your original query doesn't match the output you listed so I put a dummy subquery there but you should get the idea how to do it.
acton
acton
i think paq got it first :)
select cph.reoccupancy_phase,
itr.approved,itr.public_ho using+itr. tax_credit +itr.pbra+ itr.market total_returned,
lead(itr.eligible_to_retur n,1) over (order by cph.reoccupancy_phase) + itr.approved - (itr.public_housing+itr.ta x_credit+i tr.pbra+it r.market)
from pmco.communities com,
pmco.community_occ_phase cph,(select m.hoh_project_no, count(m.hoh_ss_number) total_households
from pmco.hoh_master m group by m.hoh_project_no) mas,
pmco.itr_eligible_to_retur n_v itr
where com.affected_rel_community = 'Yes'
and com.community_project_numb er = :PROJECT_NO
and com.community_project_numb er = cph.community_id
and com.community_project_numb er = mas.hoh_project_no
and com.community_project_numb er = itr.project_no
and itr.return_phase = cph.reoccupancy_phase
order by 1
itr.approved,itr.public_ho
lead(itr.eligible_to_retur
from pmco.communities com,
pmco.community_occ_phase cph,(select m.hoh_project_no, count(m.hoh_ss_number) total_households
from pmco.hoh_master m group by m.hoh_project_no) mas,
pmco.itr_eligible_to_retur
where com.affected_rel_community
and com.community_project_numb
and com.community_project_numb
and com.community_project_numb
and com.community_project_numb
and itr.return_phase = cph.reoccupancy_phase
order by 1
You're right buddy:
APPROVED-TOTAL_RETURNED+LE AD(ELIGIBL E_TO_RETUR N) OVER ( ORDER BY OCC_PHASE ) NEXT_PHASE
APPROVED-TOTAL_RETURNED+LE
all credits to paq. idea was gotten by paq first.
ASKER
Just for my education.. what does the lead and over statements do? I've never seen that used before.
Lead is an analytical function that allows you to look at the value of next row without a self join.
Refer to this
http://www.akadia.com/services/ora_analytic_functions.html#LEAD
Refer to this
http://www.akadia.com/services/ora_analytic_functions.html#LEAD
Thanks to all!
select occ_phase, ..... , approved - total_returned- lead(eligible_to_return) over (order by occ_phase) next_phase
from
(select occ_phase, eligible_to_return, approved, total_returned from XXXX)
/