I have a parent table called "proposal" where each proposal can have one to many "proposal_lines". A "prospect" can have one to many "proposals"
I have a query where I need to select proposals to show in a list. Within that query, I need to sum the price of each of the line items (proposal_ln). However, some of the line items could be marked as deleted. I do not want those included in the sum.
The query shown below finds all the proposals I need and sums up the prices of the line items correctly. However, this query will not show proposals where all of the proposal lines have been marked as deleted. This is because of the WHERE clause that checks the status of the "proposal_ln". I need to show a proposal whether or not it has all of its proposal_ln rows marked as deleted.
Somehow, I think need to have that WHERE clause in the "SUM(ln_final_extd_price)"
section (an inner select??) so that the outside query still gives me ALL of the proposal records, but the SUM will ONLY sum the proposal line items that aren't marked as deleted.
SELECT proposal.proposal_id,proposal.proposal_rev,prospect.prospect_name,proposal.crt_ts,proposal.proposal_eff_end_dt, SUM(ISNULL(proposal_ln.ln_final_extd_price, 0)) as final_extd_price,(ROW_NUMBER() OVER (ORDER BY proposal.crt_ts desc)) AS RecID
INNER JOIN prospect on proposal.prospect_id=prospect.prospect_id
LEFT OUTER JOIN proposal_ln ON proposal.proposal_id=proposal_ln.proposal_id
WHERE proposal.prmry_sls_prsn = 'SALES'
AND proposal.crt_ts > DATEADD(dd,-30,getDate())
and proposal.proposal_stat_cd IN ('INCP','REDY','DLVD','EXPD')
and ISNULL(proposal_ln.ln_stat_cd,'D') <> 'DLTD'
GROUP BY proposal.proposal_id