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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Min Date not pulling by client? Something else?
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 11/29/2013 :  16:07:49  Show Profile  Reply with Quote
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,
(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,

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 11/30/2013 :  01:33:59  Show Profile  Reply with Quote
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
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000