473,396 Members | 2,140 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,396 software developers and data experts.

self join query in access

dear respected gurus,

I would like to knew how to apply append,insert query for a self table
where no primary keys issues.

i do have problem say there are 5 rows of single record, this is data
of motor under one policy. say policy is john2004, so john is owner of
5 cars. now on next year the card has got renew with same
conditions(same amounts all), so i need to insert new recocrd via
query in new policy is john2005, with same condition no change

as the policy renew at masters level but on child table does not
renew...

i have been trying since 3 days and last i decided to consult with my
GURUS

i did try a lots but does not works. like

=>insert into tblMotor values (Select * from tblMotor where
policyno="john2004" from tblmotor);

=>insert into tblMotor (Select * from tblMotor where
policyno="john2004");
NOTE: above two are wrong, as renew policy is coming from other system
and there are records in master but child is empty
=>insert into tblMotor (policyno,plateno,suminsured,premiuum)
values(policyno,plateno,suminsured,premiuum);

any hints or tips for self join queries will be appreciated
rgds
shahzad
Nov 13 '05 #1
4 4440
On 26 Nov 2004 05:36:48 -0800, sh*********@gmail.com (Shahzad) wrote:
dear respected gurus,

I would like to knew how to apply append,insert query for a self table
where no primary keys issues.

i do have problem say there are 5 rows of single record, this is data
of motor under one policy. say policy is john2004, so john is owner of
5 cars. now on next year the card has got renew with same
conditions(same amounts all), so i need to insert new recocrd via
query in new policy is john2005, with same condition no change
What's the Primary Key? You say "five rows of single record" - surely
you have two tables, Policy related one to many to tblMotor?
as the policy renew at masters level but on child table does not
renew...

i have been trying since 3 days and last i decided to consult with my
GURUS

i did try a lots but does not works. like

=>insert into tblMotor values (Select * from tblMotor where
policyno="john2004" from tblmotor);
Well, you have an extra clause "from tblmotor" there which is
incorrect syntax...
=>insert into tblMotor (Select * from tblMotor where
policyno="john2004");
and this would certainly fail since by selecting * (all fields) you're
going to be trying to add a duplicate primary key.
NOTE: above two are wrong, as renew policy is coming from other system
and there are records in master but child is empty
=>insert into tblMotor (policyno,plateno,suminsured,premiuum)
values(policyno,plateno,suminsured,premiuum);


But Plateno surely isn't in tblPolicy is it?

I'm trying to understand what fields exist in your two tables, and
where the data for the new record should come from. It would also help
to know what is the Primary Key of tblMotor.

John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
Nov 13 '05 #2
In my world, self join tables must have a primary key on the child for
parent/child relationships to exist.
p.s I'm not a guru, I'm an Applications Developer.
"Shahzad" <sh*********@gmail.com> wrote in message
news:28*************************@posting.google.co m...
dear respected gurus,

I would like to knew how to apply append,insert query for a self table
where no primary keys issues.

i do have problem say there are 5 rows of single record, this is data
of motor under one policy. say policy is john2004, so john is owner of
5 cars. now on next year the card has got renew with same
conditions(same amounts all), so i need to insert new recocrd via
query in new policy is john2005, with same condition no change

as the policy renew at masters level but on child table does not
renew...

i have been trying since 3 days and last i decided to consult with my
GURUS

i did try a lots but does not works. like

=>insert into tblMotor values (Select * from tblMotor where
policyno="john2004" from tblmotor);

=>insert into tblMotor (Select * from tblMotor where
policyno="john2004");
NOTE: above two are wrong, as renew policy is coming from other system
and there are records in master but child is empty
=>insert into tblMotor (policyno,plateno,suminsured,premiuum)
values(policyno,plateno,suminsured,premiuum);

any hints or tips for self join queries will be appreciated
rgds
shahzad

Nov 13 '05 #3
dear Gurus,

yes there is master/details is there, plateno is part of details,
where is policyno is primary key in master tables and relation is
1-to-Many

now the question is this this is a self join query, we issue a policy
in master, policyno=A0604J0205,where there are no details available,
whoever i have cascade update also between this two tables

now i need to run such query which will collect(select all records
from details of A0406J0204), and insert/append AS A0604J0205

this is VBA query, if any one send any kind of normal query will also
sufficent/fruitfull
+++++++++++++++++++++++++++++++++++++
Public Function Motor_Insert2()

strSQL = "INSERT INTO tblDetails (POLICYNO, VEHICLES, MODEL, PlateNo )
" & _
" SELECT " & _
" tblDetails.POLICYNO, tblDetails.VEHICLES, tblDetails.MODEL,
tblDetails.PlateNo " & _
" FROM tblDetails " & _
" WHERE (((tblDetails.POLICYNO)= '" & A0604J0205 & "')); "

DoCmd.SetWarnings False
DoCmd.RunSQL strSQL
End Function
++++++++++++++++++++++++++++++++++++++
Nov 13 '05 #4
On 27 Nov 2004 00:07:35 -0800, sh*********@gmail.com (Shahzad) wrote:
dear Gurus,

yes there is master/details is there, plateno is part of details,
where is policyno is primary key in master tables and relation is
1-to-Many

now the question is this this is a self join query, we issue a policy
in master, policyno=A0604J0205,where there are no details available,
whoever i have cascade update also between this two tables
I do not understand. A "Self Join Query" is one in which you create a
Query by adding a table to the query grid TWICE, joining one instance
of the table to the other instance of the table. This is sometimes
useful in a heirarchy or other special circumstances; but I do not see
how a self join is useful or relevant in this case.
now i need to run such query which will collect(select all records
from details of A0406J0204), and insert/append AS A0604J0205
This would just be a simple Append query. You can append from a table
into the same table; this is *NOT* considered a "self join" query.
Maybe that's where the confusion is coming from!
this is VBA query, if any one send any kind of normal query will also
sufficent/fruitfull
+++++++++++++++++++++++++++++++++++++
Public Function Motor_Insert2()

strSQL = "INSERT INTO tblDetails (POLICYNO, VEHICLES, MODEL, PlateNo )
" & _
" SELECT " & _
" tblDetails.POLICYNO, tblDetails.VEHICLES, tblDetails.MODEL,
tblDetails.PlateNo " & _
" FROM tblDetails " & _
" WHERE (((tblDetails.POLICYNO)= '" & A0604J0205 & "')); "


This will find all of the existing records in tblDetails where
PolicyNo is A0604J0205 and append them into tblDetails. Of course
there are two problems here: 1) there aren't any records with that
policy number in tblDetails (yet); and 2) if there were, you couldn't
append them because they'd violate the Primary Key constraint!

Try:

strSQL = "INSERT INTO tblDetails " & _
" (POLICYNO, VEHICLES, MODEL, PlateNo )" & _
" SELECT 'A0604J0205' AS PolicyNo, " & _
" tblDetails.VEHICLES, tblDetails.MODEL,tblDetails.PlateNo " & _
" FROM tblDetails " & _
" WHERE (((tblDetails.POLICYNO)= '" & A0604J0204 & "')); "

This will *search* for the data from policy A0604J0204, and construct
a new record with a new policy number, A0604J0205.
John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
10
by: Rada Chirkova | last post by:
Hi, at NC State University, my students and I are working on a project called "self-organizing databases," please see description below. I would like to use an open-source database system for...
4
by: PC Datasheet | last post by:
A query based on TblEmployee shows TblEmployee in the query window. Is there a way in design view to change this query to self join T1 and T2 on EmployeeID or does it have to be typed in in SQL...
6
by: davegb | last post by:
I'm trying to create a self-join table to show the relationship between employee and supervisor. In another thread, I was advised to create a SupervisorID in the employee table, a separate...
14
by: Jim Andersen | last post by:
I have a problem with this standard employee-supervisor scenario For pictures: http://www.databasedev.co.uk/self-join_query.html I want to show all employees "belonging" to a specific...
3
by: Genalube | last post by:
All right I am new at access but why would my calculated fields in my query self destruct all of a sudden? I tested these expressions, saved the query and closed it. I reopened them and tested it. ...
2
by: Darragh | last post by:
Hi all! I'm having a bit of an issue making a self join in access (similar to the example explained on Allen Browne's excellent site - http://allenbrowne.com/ser-06.html). I've made the self...
3
by: michaeltro | last post by:
I need help with an Access query. I have a table in access with stacked data in the following format: ID Unit Job Group Score 1 GA 1A Female 5 2 GA 1A ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.