Results 1 to 4 of 4
  1. #1
    Rpuzwebb is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2011
    Posts
    2

    Customers unsold this month Query

    Hi


    I have 2 tables, Customers & Orders linked by IDOrder.

    I can easily create a list of orders made this month, but to create a list of customers I need to contact who have NOT placed orders this month seems more difficult.

    I have tried the below:
    SELECT DISTINCT [Surname] & ", " & [Firstnames] & ". " & [add1] & ", " & [Add2] & ", " & [town] AS Name, Customers.Tel, Orders.Retail
    FROM Customers INNER JOIN Orders ON Customers.ID = Orders.DistID
    WHERE (((Customers.DistCust)=2) AND ((Orders.Retail)=0)) AND Month([Orders]![Date])<>Month(Date()) And Year([Orders]![Date])<>Year(Date());

    Which returns nothing. There ARE customers in the database which meet my criteria.

    If I change the above to:
    SELECT DISTINCT [Surname] & ", " & [Firstnames] & ". " & [add1] & ", " & [Add2] & ", " & [town] AS Name, Customers.Tel, Orders.Retail
    FROM Customers INNER JOIN Orders ON Customers.ID = Orders.DistID
    WHERE (((Orders.Retail)=0) AND ((Customers.DistCust)=2) AND ((Month([Orders]![Date]))=Month(Date())) AND ((Year([Orders]![Date]))=Year(Date())));

    I still get nothing. I either get nothing or a list of customers who have placed orders at other times which will include those who have ordered this month!

    Any ideas? Thanks Richard

  2. #2
    ConneXionLost's Avatar
    ConneXionLost is offline Simulacrum
    Windows XP Access 2003
    Join Date
    Jan 2010
    Location
    Victoria, Canada
    Posts
    291
    Hi Richard,

    Try this example:

    Code:
    SELECT tblCustomer.*
    FROM tblCustomer LEFT JOIN (SELECT tblOrder.CustomerID
    FROM tblOrder
    WHERE tblOrder.Retail=0 AND Month(tblOrder.OrderDate)=Month(Date()) AND Year(tblOrder.OrderDate)=Year(Date())) AS qryCC ON tblCustomer.CustomerID = qryCC.CustomerID
    WHERE tblCustomer.DistCust=2 AND qryCC.CustomerID Is Null;
    Change table/field names where necessary, but I recommend you avoid using "ID" as a name for a Primary Key field. In this example I switched to "CustomerID". I also recommend you avoid using "Date" as a name for any field. "Date" is a reserved VBA/SQL code word.

    Cheers,

  3. #3
    Rpuzwebb is offline Novice
    Windows XP Access 2003
    Join Date
    Jan 2011
    Posts
    2
    Thank you for the above. Just trying to make it work now.

    Looks a more elegant solution to the effort I eventually came up with using 2 queries as below:

    SELECT DISTINCT [Customers]![Surname] & ", " & [Customers]![Firstnames] & ". " & [Customers]![Add1] & ", " & [Customers]![Add2] & ", " & [Customers]![Town] AS Name, [Customers]![Tel] AS Tel, Customers.ID
    FROM Customers, qryCurrentSales
    WHERE (((Customers.ID)<>All (SELECT [qryCurrentSales]![ID] FROM qryCurrentSales )) AND ((Customers.DistCust)=2))
    ORDER BY [Customers]![Surname] & ", " & [Customers]![Firstnames] & ". " & [Customers]![Add1] & ", " & [Customers]![Add2] & ", " & [Customers]![Town];

    QryCurrentSales is as below:

    SELECT [Customers]![Surname] & " " & [Customers]![Firstnames] AS Name, Orders.Retail, Orders.Date, Orders.OrdID, Customers.ID
    FROM Customers INNER JOIN Orders ON Customers.ID = Orders.DistID
    WHERE (((Orders.Retail)>0) AND ((Month([Orders]![Date]))=Month(Date())) AND ((Year([Orders]![Date]))=Year(Date())) AND ((Customers.DistCust)=2));

    Thanks very much for your efforts - Richard

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Rpuzwebb,
    If you are satisfied with your results, how about using the Thread Tools and marking this thread as Solved?

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 12-09-2010, 10:55 PM
  2. Replies: 5
    Last Post: 11-15-2010, 06:12 PM
  3. % Change by Month in a Cross-tab Query
    By William McKinley in forum Queries
    Replies: 0
    Last Post: 07-10-2010, 11:45 AM
  4. SQL Query by day to end of month
    By tcasey in forum Queries
    Replies: 0
    Last Post: 10-07-2008, 09:55 PM
  5. Delete query, deleting customers froma table.
    By keithsrobinson in forum Queries
    Replies: 2
    Last Post: 02-14-2006, 11:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums