Last 30 days in Query

  • Thread starter Thread starter FpwL1399
  • Start date Start date
F

FpwL1399

Hi, I would like for my query to only take the past 30 or 50 days of
information out of a table. The thing is that there are multiple
entries on each day...I don't know if that will effect it or not.
Thanks ahead.
 
FpwL1399,

Yes that will go back to June.

In case you didn't already know this, Excel and Access store dates as numbers and just formats them as a date. Today's date is
38897 (6-29-2006), tomorrow's is 38898 (6-30-2006). By doing that, you can easily use dates in calculations. In that formula Karl
wrote, it is telling Access to get use all dates that are greater than the date of 30 days prior to today's date (that is what
Date() returns--today's date--more appropriately, the current system date--so if you run this query tomorrow, Date() will return
tomorrows date). So on July 1st >Date()-30 will evaluate to:
38899-30 (38899 = 7-1-2006)
38869
#6/1/2006#

Now on Aug 1st, the formula will evaluate to >#7/2/2006# because July has 31 days and you are only subtracting 30 days.

I hope this helps,

Conan Kelly
 
No, I didn't know that and it is very interesting...I think I had an
experience in excel with that when I had a box formatted for a date and
I entered a 4 or 5 digit number and it jumped to a date...it didn't
make sense to me at all at the time, but now it does completely.

So what about the last 30 entries though? I'd like to be able to do
either or depending on what I'm running a query on.
 
Excel and Access use slightly different systems for numbering the dates.
The number 0 is Jan. 1,1900 in Excel, but it is Dec. 30, 1899 in Access. I
can't imagine a rational reason why it was done that way.

For the top 30 (or whatever), with the query open in design view click View
Properties. Next to Top Values put the number 30 (or whatever you want).
This assumes that the records are sorted by date (if you are looking for the
30 most recent entries).
 
FpwL1399,

In order to do the last 30 entries, you would have to first decide on an order to sort your data. If you want to sort the data by
date, that is fine.

But what happens if there are 20 records or each date? I will get all of yesterdays records (provided no entries for today has been
made yet) and then only half of the records for the day before yesterday. Now if you don't care which 10 of those 20 records are
used then you will be okay. But if you want a specific 10, you will have to find another field to sort on. On the other hand, if
you have a date field and a time field (or a field with date and time combined together in one), then you can sort by date and time
together and grab the last 30 that way.

I think this might work:

SELECT TOP 30 *
FROM TableName
ORDER BY DateField DESC

Now this may not work also. This might return the first 30 records in the table, unsorted, and then sort those 30 records in
descending order. If that is the case, then you might try this:

First create a subquery of the table sorted in descending order (maybe name it qrySubQuery or qryLast30Records_SubQuery). The SQL
syntax will be something like this:
SELECT *
FROM TableName
ORDER BY DateField DESC;

Then you can create your query based on that subquery (instead of basing it on the table directly--name it what ever is meaningful
to you). The syntax might be something like this:
SELECT TOP 30 *
FROM qrySubQuery (or qryLast30Records_SubQuery or what ever name you used before)
ORDER BY DateField;

***CAUTION: These SQL statements were manually typed in. I did not copy-n-paste. Hopefully they are correct.

If you are asking "Where do I enter these SQL statements at?", you need to enter them in SQL view of the query. The query has
Design view, Datasheet view (results of the query), and SQL view. The SQL view is also a design view, but instead of being
GUI-esque, it is more like a command line where you can type in (or paste in) the statements. When ever you design a query in the
Design view, you can switch to SQL view and Access will automatically generate the SQL statements that are equivalent to your
graphical design. In order to go to SQL view, click "View" on the Menu Bar and then select SQL view.

If these SQL statements are written correctly, you can paste them into SQL view (but changing the table and field names accordingly)
and then switch to Design view and Access will automatically construct the graphical design (as long as the syntax is correct).

You'll also notice that I used the TOP clause in the SQL statements. There is no BOTTOM clause or MIDDLE clause. So in order to
get the last 30 records, you first need to sort your data in descending order so the last 30 records you want will actually be the
first 30 records in the results of your query, and then you can grab the TOP 30 and sort them any way you want. And if you want a
certain number of records from the middle, it gets even more complicated with 3 or 4 + levels of subqueries.

If you are wondering how to use the TOP clause in Design view, on the tool bar, there is a drop-down combo box. There are set
values in there of 5, 25, 100, 5%, 25%, and All. These translate to the TOP 5 records, TOP 25 records, TOP 100, TOP 5 PERCENT, and
TOP 25 PERCENT. You can also type in whatever value that you want in this combo box. If you want the first 30 records, then type
30. If you want the first third of the whole recordset, then type 33%.

I hope this helps,

Conan Kelly
 
Hi,


To be compatible with earlier spreadsheet which wasn't aware (or didn't
care) that 1900 was NOT a leap year. So it was decided to move back by one
day to correct that boob (of earliest spreadsheet), without side effect for
dates from March First, 1900.

'Today' article at Joel on Software is just about that:
http://www.joelonsoftware.com/


Hoping it may help,
Vanderghast, Access MVP
 
Back
Top