OK, as I follow your explanation line by line, I note your reference to the
"Imported Table."
Now, nothing is as easy as it seems. I'm actually selecting my data from a
join of two tables - one is what you're calling the Imported_Table, and the
other is a table with some important dimension structure points - let's call
it the Dimension_Table. The actual Imported Table query part has other
criteria...there is a where currency_code is like "USD" and a dimension_id is
like "cc_by_mgmt_org" and amt_type_class is
in('ACCRUAL','CASH','GAAP','STATUTORY')
when I add the where date_posted (that is, of the Imported_Table) is >
Nz(max(date_posted),0) (that is, of the Main_Table)
Access returns an error telling me I can't have an aggregate function in my
where clause. I take that to mean that I can't use the max function in this
way.
Here's the actual query with all it's long actual table names and fields, if
it helps:
SELECT DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.LDR_ENTITY_ID,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.LINE_LDR_ENTITY_ID,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.JRNL_ID,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.JRNL_DESC,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.EFF_DATE,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.PERIOD,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.ACCOUNT_ID,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.COST_CENTER_ID,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.PRIMARY_ID,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.LOB_ID,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.BOB_ID,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.TYPE_CODE_ID,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.JRNL_USER_ALPHA_FLD_1,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.JRNL_USER_ALPHA_FLD_2,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.JRNL_USER_ALPHA_FLD_3,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.POSTING_YR,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.POSTING_PD,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.AMT_CLASS_1_TYPE,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.TRANS_AMT,
DM_LEDGER_DIMHIER_COST_CENTER_ID.LEVEL18_ID,
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.DATE_POSTED
FROM tbl_CY_PJL_CC_FILTER INNER JOIN
(DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE INNER JOIN
DM_LEDGER_DIMHIER_COST_CENTER_ID ON
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.COST_CENTER_ID =
DM_LEDGER_DIMHIER_COST_CENTER_ID.HIERARCHY_ID) ON
tbl_CY_PJL_CC_FILTER.COST_CENTER_ID =
DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.COST_CENTER_ID
WHERE (((DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.AMT_CLASS_1_TYPE) In
('ACCRUAL','CASH','GAAP','STATUTORY')) AND
((DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.DATE_POSTED)>Max([tbl_CY_PJL_CC_FILTER]![DATE_POSTED]))
AND ((DM_LEDGER_CURRENT_YEAR_POSTED_JRNL_LINE.TRANS_CURR_CODE)='USD') AND
((DM_LEDGER_DIMHIER_COST_CENTER_ID.DIMENSION_ID)='Cost_Centers_by_Management_Org'));
Ken Sheridan said:
That's exactly what the query Tom and I gave you should do. All you need to
do is run it at any time and it will append just those rows which have dates
later than the last one you appended.
The dialogue you are getting is a parameter prompt which means that Access
doesn't recognize the reference to the max_date_posted column. This is
presumably because that column only exists in the table you created to store
the latest date. That table isn't necessary at all to achieve what you want.
Lets take the query step by step:
INSERT INTO [Main Table]
In this line Main table is the table to which you are appending the rows.
SELECT [Imported Table].*
In this line Imported table is the table from which you are getting the rows
to be imported into Main Table.
WHERE [date_posted] >
This restricts the rows inserted to those in Imported Table with a date in a
column date_posted later than:
(SELECT NZ(MAX([date_posted]),0)
FROM [Main Table])
which is a subquery which returns the latest value in a column date_posted
in rows *already in* the table in which you are inserting the new rows.
So the end result is that only those rows with dates later than that
returned by the subquery are inserted, i.e. rows with dates *not yet in* Main
Table, which from my reading of your original post is what you want. If I've
misunderstood, and this is not what's required, perhaps you could explain it
in words of one syllable which even an Irish bogtrotter like me can
understand.
Ken Sheridan
Stafford, England
:
I really don't want to have to create a second table, but I don't clearly
understand your instructions, then. I have a table that was created to hold
the updated data. One field in that file is the date_posted. I would like to
now have just one query run each time I update the data. I don't want to
re-create all the data because it is quite large. So, I just want to use the
last date_posted field from the collected data in the table (before update)
to filter the data from the mainframe that will update my local file. I know
that I can determine what the last date_posted was. I know that I could take
that date and use it to filter the data from the mainframe so I avoid
re-creating the entire file. I'm just not clear on how to "automate" what I
am doing. As I mentioned, I tried to follow your instructions, but all I got
was a dialog asking me to put in the max_date_posted.
:
I can't see a problem, nor why you are creating the Max_date_Table at all; by
doing so you are merely introducing redundancy and the consequent risk of
inconsistent data. The latest date of rows so far appended is available from
the table into which you are appending rows with the INSERT statement. The
subquery returns this date and restricts the rows returned by the outer query
to those with values later than that date. Consequently only rows later than
that date are appended, which is what you said you wanted in your original
post.
If you insist on using the Max_date_Table then all you have to do is use
that in the subquery rather than the main table. You won't need to call the
MAX operator of course. But you'll have to update the Max_date_Table every
time so that the value represents the latest date of rows already appended,
whereas the approach described by Tom and myself dynamically identifies the
current latest date every time the query is executed.
In fact, if you do use the Max_date_Table table you don't need a subquery;
you can join the tables:
INSERT INTO [Main Table]
SELECT [Imported Table].*
FROM [Imported Table], [Max_date_Table]
WHERE [Imported Table].[date_posted] > [Max_date_Table].[max_post_date];
Ken Sheridan
Stafford, England
:
I tried this line, but there's a problem. The select query that pulls the max
date posted is pulling it out of the same table I'm appending to. Let's say
Main_Table is the one I'm appending the new data to. What I want to do is
select the last posting date of the data I have just pulled in with the
append query. Now, I want to incorporate that last date (right now I'm using
a make table query to create that max date, let's call that table
Max_date_Table, into my append query as the filter for the date_posted in the
new appended table. What I get when I use your approach is a dialog box
asking me for the max_post_date.
:
You can use a subquery to restrict the outer query to those rows whose
date_posted value is greater than the MAX(date_posted) value in the main
table. This is not the same as greater than the last 'run', as when the
query was last executed the latest date could have been earlier than the date
of execution, but that should not be relevant. The query would be along
these lines:
INSERT INTO [Main Table]
SELECT *
FROM [Imported Table]
WHERE [date_posted] >
(SELECT NZ(MAX([date_posted]),0)
FROM [Main Table]);
This assumes all columns are being inserted. If not use field lists in both
the INSERT and the SELECT clause of the outer query.
Calling the NZ function should not be necessary in your case in fact as its
only there to cater for where the Main Table would be empty, but it will do
no harm to leave it in.
Ken Sheridan
Stafford, England
:
I have made a table that will hold large amounts of financial data obtained
from a mainframe. I saved the make table query, although I only needed to use
it to make the initial table. One of the fields is a date_posted. Then, I
made a select query on the main table initially created containing only one
field and row: the max date_posted. Now, what I want to do is not remake the
main table every time I want to update the data, rather only append to it the
records that have a greater date_posted than the last run. It seems to me
that I could use the results of the last max date_posted query as a filter on
an append query for the main table, but I'm not exactly sure how to do this.
I'd like this all to run smoothly in code or from a macro in one step without
having to copy and paste the select query results into the append query.