473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Distinct Upsert Query, Access 2007

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.
Jul 24 '11 #1
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?
Jul 24 '11 #2
nico5038
3,080 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. SELECT CONF, BLD, RM, Min(DATE), count(*) as Participants
  2. FROM tblParticipants 
  3. Group by CONF, BLD, RM
  4.  
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)
Jul 25 '11 #3
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 ;-)
Jul 25 '11 #4
nico5038
3,080 Expert 2GB
Focussed or "UPSET" by UPSERT ?

<LOL>

Nic;o)
Jul 25 '11 #5
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

Expand|Select|Wrap|Line Numbers
  1. UPDATE dbo_CONF 
  2.  
  3. LEFT JOIN [Local dbo_CONF] ON 
  4. dbo_CONF.CONFERENCE_NO=[Local dbo_CONF].CONFERENCE_NO 
  5.  
  6. SET [Local dbo_CONF].CONFERENCE_NO = dbo_CONF.CONFERENCE_NO, 
  7. [Local dbo_CONF].CONF_NAME = dbo_CONF.CONF_NAME, 
  8. [Local dbo_CONF].DATE_IN = dbo_CONF.DATE_IN, 
  9. [Local dbo_CONF].DATE_OUT = dbo_CONF.DATE_OUT, 
  10. [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):

Expand|Select|Wrap|Line Numbers
  1. UPDATE [Local dbo_CONF] 
  2.  
  3. LEFT JOIN ([Local dbo_PART] 
  4. LEFT JOIN [Participant Residences] 
  5. ON ([Local dbo_PART].ROOM_NO = [Participant Residences].Residence) 
  6. AND ([Local dbo_PART].BUILDING_CODE = [Participant Residences].[Building Code]) 
  7. AND ([Local dbo_PART].CONFERENCE_NO = [Participant Residences].[Conference No])) 
  8. ON [Local dbo_CONF].CONFERENCE_NO = [Local dbo_PART].CONFERENCE_NO 
  9.  
  10. SET [Participant Residences].[Conference No] = [Local dbo_PART].[CONFERENCE_NO], 
  11. [Participant Residences].Conference = [Local dbo_CONF].[CONF_NAME], 
  12. [Participant Residences].[Conference Short Name] = [Local dbo_CONF].[UDEF02], 
  13. [Participant Residences].[Check-In] = [Local dbo_PART].[DATE_IN], 
  14. [Participant Residences].[Check-Out] = [Local dbo_PART].[DATE_OUT], 
  15. [Participant Residences].[Building Code] = [Local dbo_PART].[BUILDING_CODE], 
  16. [Participant Residences].Residence = [Local dbo_PART].[ROOM_NO];
  17.  
Somewhere, I need that GroupBy clause in this code, and I am totally lost.
Jul 25 '11 #6
nico5038
3,080 Expert 2GB
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)
Jul 25 '11 #7
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).
Jul 25 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

49
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),...
1
prn
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...
2
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...
1
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...
8
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...
3
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,...
4
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...
0
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...
10
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...
1
newnewbie
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...
0
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,...
0
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$) { } ...
0
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.