Im new to Upsert queries in Access and I have run into a problem. I have two tables, tblParticipants and tblAssignments. What I need is a query that will perform an upsert from tblParticipants to tblAssignments. For example, the following data in tblParticipants will upsert into tblAssignments as follows:
tblParticipants
CONF BLD RM DATE
1 A 100 1/1/2011
1 A 100 1/1/2011
1 A 101 2/1/2011
1 A 101 3/2/2011
1 B 100 1/1/2011
2 C 200 3/1/2011
2 A 100 4/1/2011
tblAssignments
CONF BLD RM DATE
1 A 100 1/1/2011
1 A 101 2/1/2011
1 B 100 1/1/2011
2 C 200 3/1/2011
2 A 100 4/1/2011
Essentially, there are conferences with multiple participants. Each participant is assigned a room, sometimes with a roommate. So the upsert query will take the records from tblParticipants where the BLD and RM codes are distinct for the earliest DATE, and UPSERT them into tblAssignments. Any help would be great, as I am totally lost on how to do this without temporary tables or deleting any records from a similar table. Thank you.
7 4674 NeoPa 32,556
Expert Mod 16PB
Most people are new to Upsert queries in Access Brian, as they don't exist (At least I could find no reference to them even for newer versions). The Upsert concept in Jet appears to be managed by INSERT-ing and UPDATE-ing. I would suggest using the UPDATE first, followed by the INSERT (Why bother to complicate the filtering for the UPDATE by either excluding the new records or duplicating an update of a new record?).
Does that answer your question?
Hmm, looks to me you need a GroupBy query to get the "tblAssignments".
But basically you don't need such a table, as the GroupBy query can be used instead.
Your query would look like: -
SELECT CONF, BLD, RM, Min(DATE), count(*) as Participants
-
FROM tblParticipants
-
Group by CONF, BLD, RM
-
I've added [Participants] as an extra field showing you how many participants there are for the Group By fields.
Getting the idea ?
Nic;o)
NeoPa 32,556
Expert Mod 16PB
Good point Nico. I was so focused on the UPSERT thingy I missed the obvious.
@Brian, While there may be some interesting info in my post (I hope) you should treat Nico's as the one most appropriate for your needs ;-)
Focussed or "UPSET" by UPSERT ?
<LOL>
Nic;o)
Nico, that's essentially what I am trying to do. The problem is, I can't get the data that is in the query you gave me into the tblAssignments. This is an example of another Upsert query in my DB - UPDATE dbo_CONF
-
-
LEFT JOIN [Local dbo_CONF] ON
-
dbo_CONF.CONFERENCE_NO=[Local dbo_CONF].CONFERENCE_NO
-
-
SET [Local dbo_CONF].CONFERENCE_NO = dbo_CONF.CONFERENCE_NO,
-
[Local dbo_CONF].CONF_NAME = dbo_CONF.CONF_NAME,
-
[Local dbo_CONF].DATE_IN = dbo_CONF.DATE_IN,
-
[Local dbo_CONF].DATE_OUT = dbo_CONF.DATE_OUT,
-
[Local dbo_CONF].UDEF02 = dbo_CONF.UDEF02;
The above query works just fine. What I have for the Participants Upsert query so far is this (Participant Residences is actually tblAssignments): - UPDATE [Local dbo_CONF]
-
-
LEFT JOIN ([Local dbo_PART]
-
LEFT JOIN [Participant Residences]
-
ON ([Local dbo_PART].ROOM_NO = [Participant Residences].Residence)
-
AND ([Local dbo_PART].BUILDING_CODE = [Participant Residences].[Building Code])
-
AND ([Local dbo_PART].CONFERENCE_NO = [Participant Residences].[Conference No]))
-
ON [Local dbo_CONF].CONFERENCE_NO = [Local dbo_PART].CONFERENCE_NO
-
-
SET [Participant Residences].[Conference No] = [Local dbo_PART].[CONFERENCE_NO],
-
[Participant Residences].Conference = [Local dbo_CONF].[CONF_NAME],
-
[Participant Residences].[Conference Short Name] = [Local dbo_CONF].[UDEF02],
-
[Participant Residences].[Check-In] = [Local dbo_PART].[DATE_IN],
-
[Participant Residences].[Check-Out] = [Local dbo_PART].[DATE_OUT],
-
[Participant Residences].[Building Code] = [Local dbo_PART].[BUILDING_CODE],
-
[Participant Residences].Residence = [Local dbo_PART].[ROOM_NO];
-
Somewhere, I need that GroupBy clause in this code, and I am totally lost.
Just curious why you want such a table as I stated in my first comment "But basically you don't need such a table, as the GroupBy query can be used instead."
The only reason to store the data would be to get a history when the tblParticipants isn't holding all data.
For the UPSERT (new for me, but I get the idea) I normally use two queries:
1) Delete * from tblAssignments where CONF & BLD & RM in (select CONF & BLD & RM from tblParticipants)
2) INSERT INTO tblAssignments (<the groupby query>)
Getting the idea ?
Nic;o)
NeoPa 32,556
Expert Mod 16PB Nico:
For the UPSERT (new for me, but I get the idea) I normally use two queries:
1) Delete * from tblAssignments where CONF & BLD & RM in (select CONF & BLD & RM from tblParticipants)
2) INSERT INTO tblAssignments (<the groupby query>)
I'm always hesitant to question anything Nico says (as I don't know anyone with more experience of Access than he), but the above approach might cause problems in a situation where not all the data is provided by the data that is available. This can happen when AutoNumber IDs are used for instance.
This may well not be a problem for you, but if it is then the approach outlined earlier (post #2) should still work.
PS. As Nico said Brian, there's almost no conceivable reason for maintaining a separate tblAssignments table in your database. Duplicating data like that is never recommended, and would cause your database to fail all the normalisation tests (See Database Normalisation and Table structures).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Allen Browne |
last post by:
If you are looking for opinon on what's useful in Access 2007, there's a new
article at:
http://allenbrowne.com/Access2007.html
Covers what's good (useful features), what's mixed (good and bad),...
|
by: prn |
last post by:
Hi folks,
I'm relatively new to Access, but I seem to have drawn the short straw, so I have the assignment for my workplace of looking for problems/inconsistencies in migrating applications to...
|
by: ARC |
last post by:
Just curious if anyone is having issues with Acc 2007 once the number of
objects and complexity increases? I have a fairly large app, with many
linked tables, 100's of forms, queries, reports, and...
|
by: rickcross |
last post by:
I am trying to use the Access 2007 runtime. I have a program that is
fully working in 2007 but when I install the runtime version with same
Operating system and Access 2003 installed I have...
|
by: elias.farah |
last post by:
Hello Everyone,
I'm having some very weird behavior on a couple of Access forms. (Not
all forms, just some of them).
The forms have been working for years, under Access XP/2003 etc, and
last...
|
by: cwoll |
last post by:
Hi
I need help. I have a ms access 2003 database that I would like to
upgrade to ms access 2007. The first time I opened I had to fix a few
references it wanted a DAO2535.TLB file witch I gave it,...
|
by: myemail.an |
last post by:
Hi all,
I use Access 2007 and have the following problems: when exporting
banal select queries (either to Excel or to a csv file) I find that
exporting often doesn't work and creates a file with...
|
by: raylopez99 |
last post by:
10 years ago, the below was written (see very end, after my signature
RL).
What, if anything, has changed?
I have Access 2003 and soon Access 2007 on a Windows XP Professional
or Windows...
|
by: Arno R |
last post by:
Hi all,
So I bought a new laptop 10 days ago to test my apps with Vista. (home premium)
Apparently Office 2007 is pre-installed. (a time limited but complete test version, no SP1)
So I take the...
|
by: newnewbie |
last post by:
Hi,
Short version: trying to export more than 65K of data from Access 2007 into Excel 2007 (important) through VBA. Access gives me 65K row limitation error, though I thought that was no longer...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| | |