MS Access Forum / Queries / March 2008
Return comination of Current record and next record fields
|
|
Thread rating:  |
efandango - 23 Jan 2008 18:35 GMT I have a table:
Run_point_List_ID 1 (Autonumber) Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C Run_point_Venue_D
How can I get the query to return the first two fields from the 1st record and the second two fields from the 2nd record, and so on... like the example below?
From Record 1 From Record 1 From Record 2 From Record 2 From Record 2 From Record 2 From Record 3 From Record 3 From Record 3 From Record 3 From Record 4 From Record 4 From Record 4 From Record 4 From Record 5 From Record 5 From Record 5 From Record 5 From Record 6 From Record 6
Run_point_List_ID 1 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C Run_point_Venue_D Run_point_List_ID 2 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C Run_point_Venue_D Run_point_List_ID 3 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C Run_point_Venue_D Run_point_List_ID 4 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C Run_point_Venue_D
John Spencer - 23 Jan 2008 19:37 GMT SELECT A.Run_Point_List_ID , A.Run_Point_Venue_A , A.Run_Point_Venue_B , B.Run_Point_Venue_C , B.Run_Point_Venue_D FROM Run_Point as A INNER JOIN Run_Point as B On A.Run_Point_List_ID = B.Run_Point_List_ID +1
That will work as long as Run_Point_List_ID is sequential with NO gaps.
If you have gaps then life gets more complex as you will have to calculate the next higher number. That Might look something like the following.
SELECT A.Run_Point_List_ID , A.Run_Point_Venue_A , A.Run_Point_Venue_B , B.Run_Point_Venue_C , B.Run_Point_Venue_D FROM Run_Point as A , Run_Point as B WHERE B.Run_Point_List_ID = (SELECT Min(Tmp.Run_Point_List_ID) FROM Run_Point as Tmp WHERE Tmp.Run_Point_List_ID > A.Run_Point_List_ID)
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
>I have a table: > [quoted text clipped - 23 lines] > Run_point_List_ID > 4 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C Run_point_Venue_D efandango - 23 Jan 2008 21:34 GMT John,
That is great, but not quite working the way I figured.
The 2nd half of the 1st segment is missing, as in an 'odd man out' kind of way, where the very first record's 2nd half is (naturally) displaced by the next records' 1st half. I'm just trying to take in all the permutations, as there are even more fields that I have added, and i'm trying to get my head round a screen full of data rows now.
I didn't show the additonal fields in my first post as I didn't want to make extra work for someone such as yourself. They follow the same pattern as the original fields, just that they go from A-H (first half) I-P (2nd half) of each record. in all a total of 16 fields laid out spreadsheet style. So, once I really get my head around what's what, can I come back to you on this? (will you get email notification from my response?)
> SELECT A.Run_Point_List_ID > , A.Run_Point_Venue_A [quoted text clipped - 47 lines] > > Run_point_List_ID > > 4 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C Run_point_Venue_D John Spencer - 24 Jan 2008 12:24 GMT No, I won't get notified if you post additional messages to this thread. I usually keep an eye out, but I have been known to miss a followup.
Which solution did you use? You are always going to be missing half of the fields in either the first record or the last record.
If you would rather the last record is unmatched then you need to modify the two queries - whichever one you are using
SELECT A.Run_Point_List_ID , A.Run_Point_Venue_A , A.Run_Point_Venue_B , B.Run_Point_Venue_C , B.Run_Point_Venue_D FROM Run_Point as A INNER JOIN Run_Point as B On A.Run_Point_List_ID = B.Run_Point_List_ID - 1 <<<<<<< minus 1 instead of plus 1
Max and Less than instead of Min and greater than in the subquery.
SELECT A.Run_Point_List_ID , A.Run_Point_Venue_A , A.Run_Point_Venue_B , B.Run_Point_Venue_C , B.Run_Point_Venue_D FROM Run_Point as A , Run_Point as B WHERE B.Run_Point_List_ID = (SELECT Max(Tmp.Run_Point_List_ID) FROM Run_Point as Tmp WHERE Tmp.Run_Point_List_ID < A.Run_Point_List_ID)
 Signature John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County .
> John, > [quoted text clipped - 76 lines] >> > 4 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C >> > Run_point_Venue_D efandango - 24 Jan 2008 22:07 GMT John,
ultimately Your solution was great, I made adjustments (due to my lack of foresight), but bottom line, you answered my call for help (I was truly stumped), and it worked. Yes, I will lose half the first or last records, but that doesn't matter.
Here is what I now have:
SELECT A.Run_Point_List_ID, A.Run_No,
A.Run_point_Venue_X2, A.Run_point_Address_X2, A.Run_Point_Venue_I, A.Run_Point_Address_I, A.Run_Point_Venue_J, A.Run_Point_Address_J, A.Run_Point_Venue_K, A.Run_Point_Address_K, A.Run_Point_Venue_L, A.Run_Point_Address_L, A.Run_Point_Venue_M, A.Run_Point_Address_M, A.Run_Point_Venue_N, A.Run_Point_Address_N, A.Run_Point_Venue_O, A.Run_Point_Address_O, A.Run_Point_Venue_P, A.Run_Point_Address_P,
B.Run_point_Venue_X1, B.Run_point_Address_X1, B.Run_Point_Venue_A, B.Run_Point_Address_A, B.Run_Point_Venue_B, B.Run_Point_Address_B, B.Run_Point_Venue_C, B.Run_Point_Address_C, B.Run_Point_Venue_D, B.Run_Point_Address_D, B.Run_Point_Venue_E, B.Run_Point_Address_E, B.Run_Point_Venue_F, B.Run_Point_Address_F, B.Run_Point_Venue_G, B.Run_Point_Address_G, B.Run_Point_Venue_H, B.Run_Point_Address_H
FROM tbl_Points AS A INNER JOIN tbl_Points AS B ON B.Run_Point_List_ID=A.Run_Point_List_ID+1;
Which is ok, but what I would realy like if possible is for the 2nd half fields be renamed to the first half, and vice-versa. so that ultimately Run_Point_Venue_I becomes Run_Point_Venue_A Run_Point_Venue_J becomes Run_Point_Venue_B Run_Point_Venue_K becomes Run_Point_Venue_C Run_Point_Venue_L becomes Run_Point_Venue_D and so on...
the reason for this is that I have lots of predefined forms & reports that used the old/original field orders which is going to take a long time to copy and refomat, In an Ideal world it would be great to use the same Forms & Reports and not have to maintain two different copies of each, instead just perhaps having a button, or some other mechanism to switch the SQL source.
> No, I won't get notified if you post additional messages to this thread. I > usually keep an eye out, but I have been known to miss a followup. [quoted text clipped - 107 lines] > >> > 4 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C > >> > Run_point_Venue_D efandango - 18 Mar 2008 19:01 GMT John, (after some sage advice from John W. Vinson, I have changed the table somewhat and was wondering...
Can you help with this?...
I want to find a similar solution thing to the previous problem, but the key difference here is that the 18 sets of fields are not seperated into 18 seperate fields, but have been normalized/consolidated into 1 field for Venue and 1 for Address, but each set is held 'in order' by a [OrderSeq] field that has a maximum of 18 per set of addresses.
The similar solution being I want to take the last 9 records (of each OrderSeq) and marry them up with the first 9 records of the next set of 18.
For example:
Run_No 1 (last 9) Run_No 2 (first 9) and so on...
My table: (tbl_Points) looks like this:
Run_No (Master Field) Point_ID (Primary Key) OrderSeq (Number) orders the address list sequence) Run_point_Venue (Text Run_point_Address (Text)
From a user's point of view, the records are broken down into sets of 18, denoted by a master field, Run_No. They are essentially ordered by the [OrderSeq] field.
Sample records go like this:
Run_No Point_ID OrderSeq Run_point_Venue Run_point_Address 1 1 377 2 De Vere Hotel 1 Aldwych 1 377 3 London Shelton Hotel Chancel Street 1 377 4 Costello Palace Hotel Lavington Street 1 377 ...18 Hilton Hotel Tower Bridge Savoy Court 2 378 1 Bruebaker Hotel 10 John Adam Street 2 378 2 Parkside Hotel N4 Cheney Road 2 378 3 Elena Hotel Leicester Square 2 378 ...18 Hilton Tower Bridge Hotel Savoy Place 3 379 1 Celstion Hotel 18 Northumberland Avenue 3 379 2 Pembury Hotel Drury Lane 3 379 3 Eric Hotel Lennox Road 3 379 ...18 Holiday Inn Express SE1 Seven Sisters Road
> No, I won't get notified if you post additional messages to this thread. I > usually keep an eye out, but I have been known to miss a followup. [quoted text clipped - 107 lines] > >> > 4 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C > >> > Run_point_Venue_D John Spencer - 18 Mar 2008 21:16 GMT To get the last nine to match one for one to the first nine of the next group, you would need something like the following.
SELECT A.Run_No, B.Run_No , A.Point_ID, B.Point_ID , A.OrderSeq, B.OrderSeq FROM RunTable as A INNER JOIN RunTable as B ON A.Run_No + 1 = B.Run_No AND A.OrderSeq - 9 = B.OrderSeq
By the way, Point_ID cannot be the primary key since by definition no two records can have the same value for the primary key field.
If I understood correctly, that should return 1,2,377,378,10,1 1,2,377,378,11,2 1,2,377,378,12,3 ...
'==================================================== John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County '====================================================
> John, (after some sage advice from John W. Vinson, I have changed the table > somewhat and was wondering... [quoted text clipped - 158 lines] >>>>> 4 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C >>>>> Run_point_Venue_D efandango - 18 Mar 2008 23:51 GMT John,
That is so nearly there, well so far your SQL has succesfully put them all into the correct order, but I could only see the various record, Id, etc numbers on the query result, so I added the text fields like this:
SELECT A.Run_No, B.Run_No, A.Point_ID, B.Point_ID, A.OrderSeq, B.OrderSeq, A.Run_point_Venue, A.Run_point_Address, B.Run_point_Venue, B.Run_point_Address FROM tbl_Points as A INNER JOIN tbl_Points as B ON A.Run_No + 1 = B.Run_No AND A.OrderSeq - 9 = B.OrderSeq
WHICH gives me this:
A.Point_ID A.Run_point_Venue A.Run_point_Address A.OrderSeq A.Run_No B.Run_No B.Point_ID B.OrderSeq B.Run_point_Venue B.Run_point_Address 11 De Vere Hotel 1 Aldwych 10 1 2 19 1 Hilton Hotel Tower Bridge Savoy Court 10 London Shelton Hotel Chancel Street 11 1 2 20 2 Bruebaker Hotel 10 John Adam Street 12 Costello Palace Hotel Lavington Street 12 1 2 21 3 Parkside Hotel N4 Cheney Road
BUT what I need is for the B.Run_Point_Venue and B.Run_point_Address to merge with the A.Run_Point_Venue and A.Run_point_Address respectively, with the B.Run_Point_Venue going before the A.Run_Point_Venue.
So that I end up with the fields and records like this:
Run_No Point_ID OrderSeq Run_point_Venue Run_point_Address 2 19 1 Hilton Hotel Tower Bridge Savoy Court 2 20 2 Bruebaker Hotel 10 John Adam Street 2 21 3 Parkside Hotel N4 Cheney Road 2 11 10 De Vere Hotel 1 Aldwych 2 10 11 London Shelton Hotel Chancel Street 2 12 12 Costello Palace Hotel Lavington Street
> To get the last nine to match one for one to the first nine of the next > group, you would need something like the following. [quoted text clipped - 184 lines] > >>>>> 4 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C > >>>>> Run_point_Venue_D John Spencer - 19 Mar 2008 00:52 GMT I really don't understand the results you want. It seems like you are saying that you want a sort order of the records so that they appear in a certain order.
I was pairing the records together in one result.
You will have to play around a bit with Point_ID and OrderSeq
The basic idea would be something like:
SELECT Run_No, Point_Id, OrderSeq , Run_Point_Venue , Run_Point_Address FROM tbl_Points ORDER BY IIF(OrderSeq > 9, Run_No, Run_No-1), OrderSeq
I'm not sure that will give you what you want, but you should be able to experiment a bit to get what you are attempting. It might be that you need to add 1 instead of subtract 1 or that ...
Good luck
'==================================================== John Spencer Access MVP 2002-2005, 2007-2008 Center for Health Program Development and Management University of Maryland Baltimore County '====================================================
> John, > [quoted text clipped - 222 lines] >>>>>>> 4 Run_point_Venue_A Run_point_Venue_B Run_point_Venue_C >>>>>>> Run_point_Venue_D efandango - 19 Mar 2008 01:33 GMT John,
What I am trying to arrive at is just one overall consolidated set of:
Run_point_Venue Run_point_Address
instead I have two sets like this:
A.Run_point_Venue A.Run_point_Address B.Run_point_Venue B.Run_point_Address
> I really don't understand the results you want. It seems like you are > saying that you want a sort order of the records so that they appear in [quoted text clipped - 249 lines] > >>>>>>> Run_point_Venue_D > >>>>>>> Run_point_List_ID
|
|
|