Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS Access: how to make a complex query

Posted on 2009-07-15
3
Medium Priority
?
347 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

877 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