Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to SUM() a field of a subset of child records in a query?

Posted on 2008-10-08
2
Medium Priority
?
152 Views
Last Modified: 2012-06-27
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.

Thoughts?
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 
  FROM [dbo].[proposal]
INNER JOIN prospect on proposal.prospect_id=prospect.prospect_id
LEFT OUTER JOIN proposal_ln ON proposal.proposal_id=proposal_ln.proposal_id
and proposal.proposal_rev=proposal_ln.proposal_rev
 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
      ,proposal.proposal_rev
      ,prospect_name
	  ,proposal.crt_ts
	  ,proposal.proposal_eff_end_dt 
	  ,stat.stat_desc

Open in new window

0
Comment
Question by:bwaldhart
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Accepted Solution

by:
mdougan earned 500 total points
ID: 22670859
Well, you could use a CASE statement within the SUM

SUM(
           CASE WHEN proposal_ln.ln_final_extd_price IS NULL THEN 0
                     WHEN proposal_ln.status = 'DELETED' THEN 0
                     ELSE proposal_ln.ln_final_extd_price
           END
        ) as final_extd_price

That way, you'd just be summing zero which wouldn't change the amount.
0
 

Author Closing Comment

by:bwaldhart
ID: 31504317
You're da man!  Sometimes it just takes a second set of eyes to point out a great, obvious solution.  Thanks!!!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question