Posted - 11/29/2013 : 16:07:49
| Hi all, would greatly appreciate any light you can shine on the issue below...
Two tables are used, one with visits made to an account (ditributor id, client id and visit date) and a second with sales records (same distributor and client info with sales dates and $). I would like to zero out the sales for account that do not have at least 9 months of sales before a visit.
problem is it is giving me a zero for every record! Problem is with line 3, where I pull the Min(SaleDate)... I think the issue is that I am not defining the join between the DistribID and the ClientID within that somehow, but I am unsure....
The second case seems to work, just saying I need at least 9 months of distance between a visit and a "loyaltydate" (date they became a preferred customer)....any ideas?
(select Case when DATEDIFf(MM,VisitDate,MIN(SaleDate))<=9 then 0
Else Case when DATEDIFF(MM,VisitDate,isnull(LoyaltyDate, DATEADD(MM,-9,VisitDate))) >= 9 then SUM(tblSalesbySalon.SaleAmnt)/6 else 0 end
from tblSalesbySalon INNER JOIN
tblBMVisits ON tblSalesbySalon.DistribID = tblBMVisits.DistribID AND tblSalesbySalon.DistribClientID = tblBMVisits.DistribClientID
WHERE DATEDIFF(MM,tblSalesbySalon.SaleDate,tblBMVisits.VisitDate) between 1 and 6
and T1.DistribID = tblBMVisits.DistribID AND T1.DistribClientID = tblBMVisits.DistribClientID
group by VisitDate, LoyaltyDate) 'PRE'
from tblBMVisits T1
group by T1.DistribID,