Solved

MS Access: how to make a complex query

Posted on 2009-07-15
3
341 Views
Last Modified: 2012-05-07
Hi X-perts,

This is a structure of my table:

return                              date                                 name
0.9                                   1/1/2005                         assetA
0.8                                   2/1/2005                         assetA
0.4                                   3/1/2005                         assetA
0.66                                 4/1/2005                        assetA

0.76                                 6/1/2007                         assetB
0.26                                  7/1/2007                        assetB
0.87                                  8/1/2007                        assetB

etc etc
The column [date] presents consecutive sets of dates for each asset. I need to make the query which selects all records with returns later than a certain date EXCEPT these that don't have returns for the last three months (deselecting non-reporting fund managers for the last 3 months).

So, the query:

1) selects all records with returns later than 4/1/2005. This is a simple thing

SELECT * FROM mytable WHERE [date] > #4/1/2005#

2) also should remove records that don't exist after certain date (3 months before the current date). I can filter these records by

SELECT * FROM mytable WHERE [date] >   DateAdd("m", -4, Date)

but this deselects all records before that date.

How can I do both filters in one query?

thanks
0
Comment
Question by:andy7789
3 Comments
 
LVL 5

Expert Comment

by:Aaronroach
ID: 24865274
For ease i tend to do this sort of thing in the Access Query browser first and then just get the SQL afterwards (by changing mode to SQL)

What you would do i pull the date field in the field selection area, then in criteria put:

Between #4/1/2005# and DateAdd("m", -4, Date)

Although in your query above you have put > for both these points, i take it you actually want all records between 4/1/05 and Today - 4months.

If thats what you want then the above is what you need.
0
 

Author Comment

by:andy7789
ID: 24865355
I don't have any query browsers - all done via VBA from Excel. It has to be only a VBA querie
0
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 24866863
Select * from mytable inner join
(Select distinct [Name] from mytable where [date] >DateAdd("m", -4, Date) ) as q1
on mytable.[name] =q1.[Name]
Where mytable.[date]>#4/1/2005#

You should not use Name or Date as fieldnames.  They are reserved words.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question