Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with UNION query? 1

Status
Not open for further replies.

VickyC

Technical User
Sep 25, 2010
206
0
0
CA
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		[b][COLOR=#CC0000]1	-1	0	0[/color][/b]
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		[b][COLOR=#CC0000]2	-1	0	0[/color][/b]
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		[b][COLOR=#CC0000]3	-1	0	0[/color][/b]
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
 
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:

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
 
hey Duane - Works perfectly!
Thanks, Vicky
 
Why GROUP BY? Wouldn't that do the trick? [PONDER]

Code:
INSERT INTO tbl_OLD ( ID, Rank, X, Y )
SELECT [BLUE]DISTINCT[/BLUE] ID, -1, 0, 0
FROM tbl_OLD


---- Andy

There is a great need for a sarcasm font.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top