×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Help with UNION query?

Help with UNION query?

Help with UNION query?

(OP)
hi

I have a huge table like the one at LEFT. For each ID, Rank runs from 0 to 4. The ID increments by 1 after each set of 5 records.
I want to insert a new record at the beginning of each group of 5 records. This is shown at RIGHT. For each of these new records, the last 3 fields are -1, 0, 0.

CODE

tbl_OLD                                 tbl_NEW 
~~~~~~~                                 ~~~~~~~
ID	Rank	X	Y		ID	Rank	X	Y
===========================             ===========================
1	0	18	96		1	-1	0	0
1	1	65	100		1	0	18	96
1	2	39	50		1	1	65	100
1	3	58	23		1	2	39	50
1	4	25	103		1	3	58	23
2	0	39	50		1	4	25	103
2	1	65	100		2	-1	0	0
2	2	18	96		2	0	39	50
2	3	58	23		2	1	65	100
2	4	25	103		2	2	18	96
3	0	18	96		2	3	58	23
3	1	89	34		2	4	25	103
3	2	25	103		3	-1	0	0
3	3	58	23		3	0	18	96
3	4	39	50		3	1	89	34
					3	2	25	103
					3	3	58	23
					3	4	39	50 

I'm guessing this involves a UNION query, but I'm rusty enough that I cant get it to work.
Any help is appreciated

Vicky

RE: Help with UNION query?

There is no " insert a new record at the beginning of each group of 5 records" since records are like marbles in a box. There is no order.

You can probably create a query to run once:

CODE --> SQL

INSERT INTO tbl_OLD ( ID, Rank, X, Y )
SELECT tbl_OLD.ID, -1 AS NewRank, 0 AS NewX, 0 AS NewY
FROM tbl_OLD
GROUP BY tbl_OLD.ID, -1, 0, 0; 

Then order the results by ID and Rank

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

RE: Help with UNION query?

(OP)
hey Duane - Works perfectly!
Thanks, Vicky

RE: Help with UNION query?

Why GROUP BY? Wouldn't that do the trick? PONDER

CODE

INSERT INTO tbl_OLD ( ID, Rank, X, Y )
SELECT DISTINCT ID, -1, 0, 0
FROM tbl_OLD 


---- Andy

There is a great need for a sarcasm font.

RE: Help with UNION query?

What can I say? I like my GROUP BYs winky smile

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close