tags:

views:

50

answers:

3

In access I wrote this query:

Select
  I.sysid, I.MemberNumber, I.Date, I.Distributer,
  F.MemberNumber as FMember, F.Date as FDate, I.Distributer as FDistributer
From Initial as I
Left Join Final as F ON
  I.MemberNumber=F.MemberNumber and
  I.Distributer=F.Distributer and
  I.Date>=F.Date-14 and
  I.Date<=F.Date+14;

But the left join is not behavior properly. There are fewer rows in this table then there are in Initial... but it should be keeping ALL rows from initial, because I am using a left join, right? I have found several rows in initial (like sysid=7, which is Initial's key) that just isn't coming into this table.

+2  A: 

It may have to do with your AND logic. Add some ( ) parenthesis to this to include it all like so:

Select
  I.sysid, I.MemberNumber, I.Date, I.Distributer,
  F.MemberNumber as FMember, F.Date as FDate, I.Distributer as FDistributer
From Initial as I
Left Join Final as F ON
  (I.MemberNumber=F.MemberNumber and
  I.Distributer=F.Distributer and
  (I.Date>=F.Date-14) and
  (I.Date<=F.Date+14));

Also I think there is a dateadd function, I'd use that instead of + / -.

JonH
Decent suggestion, but it didn't work. I am still not getting the line from Initial with sysID=7 on it (there are a bunch of others that don't come in either). The weird part is if I filter the results on sysID=7, it finds the line, but without the filter it doesn't exist (like if I sort it, or make it into a maketable, there is no 7). So bizarre. Any other suggestions?
Dan
@Dan - start reducing the code to the point where finding your answer is trivial. So get rid of the AND condition involving the dates first. Run the query see if you get the data. Then ensure the MemberNumber of I and F really do match..keep going from there. Surely it is something real simple (null, or not match, date issue, etc).
JonH
DateAdd() is not necessary if you date math is on days, since the integer part of Jet/ACE date fields is the day.
David-W-Fenton
+1  A: 

If you're in the Query Designer, make sure all the filters are cleared. I've built your tables and sql and can't reproduce your error.

SELECT I.sysid
    , I.MemberNumber
    , I.Dated
    , I.Distributer
    , F.MemberNumber
    , F.Dated AS FDated
    , F.Distributer AS FDistributer
FROM Initial AS I 
LEFT JOIN Final AS F 
ON I.Distributer = F.Distributer 
      AND I.MemberNumber = F.MemberNumber
      AND I.Dated>=F.Dated-14 
      AND I.Dated<=F.Dated+14;
Jeff O
A: 

Try adding additional fields to the original table and using update queries to add extra columns. This way you can be sure you won't drop any columns.

Dan