Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Min Date not pulling by client? Something else?

Author  Topic 

metube
Starting Member

1 Post

Posted - 2013-11-29 : 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 T1.DistribID,
T1.DistribClientID,
(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
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,
T1.DistribClientID

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-30 : 01:33:59
sorry cant make out much from posted query about which table has which columns and how sample data is.
Post some sample data and explain what you want as output so that we will be able to get your requirement clearly.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -