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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select based on either or

Status
Not open for further replies.

hamking01

Programmer
May 30, 2004
238
US
I trying to add up values of a column based on whether another column has a date entered as such:

SELECT
P.Plant
, ISNULL(SUM(CASE WHEN F.dt_Sign BETWEEN '01/01/00'AND
'12/31/00' THEN M.AppvdAmount END), 0) AS CurrentTotal
, ISNULL(SUM(CASE WHEN F.dt_Sign BETWEEN '01/01/00' AND
'12/31/00' THEN M.AppvdAmount END), 0) AS YTDTotal
LEFT JOIN tbl_Feedback F (NOLOCK)
ON M.tbl_MainId = F.tbl_MainId
WHERE F.IdPerson IN (8, 9)

This will work add the AppvdAmount to both the CurrentToal and YTDTotal if either F.IdPerson 8 or 9 has a Date value in dt_Sign. However, if there is a Date value for BOTH 8 and 9, it will add the AppvdAmount twice.

I only want to have it added once, so long as either or both have Data value, but not to add when both field are blank.

How would I do this?
 
How about dividing by a count value?
Code:
SUM(CASE WHEN F.dt_Sign BETWEEN '01/01/00'AND
     '12/31/00' THEN M.AppvdAmount END) /
sum(case when F.dt_Sign BETWEEN '01/01/00'AND
     '12/31/00' THEN 1 else 0 end) END AS CurrentTotal

Regards,
AA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top