SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

metube
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,
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

India
52317 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
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.05 seconds. Powered By: Snitz Forums 2000