473,387 Members | 1,536 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,387 software developers and data experts.

Query or script to sort data needed.

12
Access 2003, Windows XP Pro

Trying to take one field of values in a table select a range of records and then skip a range of records and then select a new range of records to append into a new table.

For example, if my inital field has 100,000 records in it and I want to select the first 1,000 records then skip the next 3,000 records, then select the next 1,000 records and continue this until the end of the initial field appending these 1,000 record sets into a new table.

Thanks for any help
Ron
Mar 28 '07 #1
24 1583
Rabbit
12,516 Expert Mod 8TB
What is the rhyme and reason for this?
Mar 28 '07 #2
RonVACA
12
we need to set up the data in a specific order for a printing process and the primary field has millions of records
Mar 28 '07 #3
Rabbit
12,516 Expert Mod 8TB
What I meant is if there's some sort of process you use to determine which ones need to be printed. Preferably something that relates to what's actually in the tables. Or if these numbers are constant and will never change.
Mar 28 '07 #4
RonVACA
12
The data is a fixed set of 5 million records. we have three fields in the data an ID, Serial, AlphaAlias. We are printing labels each one unique with a serial number and an alpha numeric alias each roll of printed labels is 6500 long. However we are printing four labels at a time. the printer driver needs the data in a flat file that mimicks the four across label configuration. So we need to take the single long list of records and break it into four sets of Serial, AlphaAlias pairs. The end result will be a flat table that has 9 fields with 1.25 million records. The fields will be- ID, Serial1, AlphaAlias1, Serial2, AlphaAlias2, Serial3, AlphaAlias3, Serial4, AlphaAlias4. The serial numbers in the initial data are sequential so after running the process the Serial1 column will have serial numbers 1-6500,26001-32500,52001-58500 and so on. Serial2 will have serial numbers 6501-13000, 32501-39000, 58501-65000 and so on.
Sorry for the long winded explanation.
Mar 28 '07 #5
Rabbit
12,516 Expert Mod 8TB
The data is a fixed set of 5 million records. we have three fields in the data an ID, Serial, AlphaAlias. We are printing labels each one unique with a serial number and an alpha numeric alias each roll of printed labels is 6500 long. However we are printing four labels at a time. the printer driver needs the data in a flat file that mimicks the four across label configuration. So we need to take the single long list of records and break it into four sets of Serial, AlphaAlias pairs. The end result will be a flat table that has 9 fields with 1.25 million records. The fields will be- ID, Serial1, AlphaAlias1, Serial2, AlphaAlias2, Serial3, AlphaAlias3, Serial4, AlphaAlias4. The serial numbers in the initial data are sequential so after running the process the Serial1 column will have serial numbers 1-6500,26001-32500,52001-58500 and so on. Serial2 will have serial numbers 6501-13000, 32501-39000, 58501-65000 and so on.
Sorry for the long winded explanation.
Not at all, it was necessary to clarify the situation to better understand what you are looking for. I'll get back to you unless some one beats me to the punch
Mar 28 '07 #6
RonVACA
12
Thanks in advance for the help
Mar 29 '07 #7
Rabbit
12,516 Expert Mod 8TB
Need one more piece of information, is the table sorted on one of the fields? Or does it have a field that get's larger with each succeeding record? Like an Autonumber field?
Mar 29 '07 #8
RonVACA
12
the ID field is an autonumber field and the serial field increments by one for each record.
Mar 29 '07 #9
Rabbit
12,516 Expert Mod 8TB
So the serial field starts at 1 and then increases by 1 until you get too the 100,00th record? And there are no gaps in between?

If so, then you made it a lot easier because I won't have to go through all that.
Mar 29 '07 #10
RonVACA
12
the ID field starts at 1 and goes to 5,000,000 the Serial field starts at 20,000,000 and goes to 24,999,999 and yes they increment by 1 in both fields
Mar 29 '07 #11
Rabbit
12,516 Expert Mod 8TB
The data is a fixed set of 5 million records. we have three fields in the data an ID, Serial, AlphaAlias. We are printing labels each one unique with a serial number and an alpha numeric alias each roll of printed labels is 6500 long. However we are printing four labels at a time. the printer driver needs the data in a flat file that mimicks the four across label configuration. So we need to take the single long list of records and break it into four sets of Serial, AlphaAlias pairs. The end result will be a flat table that has 9 fields with 1.25 million records. The fields will be- ID, Serial1, AlphaAlias1, Serial2, AlphaAlias2, Serial3, AlphaAlias3, Serial4, AlphaAlias4. The serial numbers in the initial data are sequential so after running the process the Serial1 column will have serial numbers 1-6500,26001-32500,52001-58500 and so on. Serial2 will have serial numbers 6501-13000, 32501-39000, 58501-65000 and so on.
Sorry for the long winded explanation.
Makes things easier, you'll need 4 different append queries for those 8 fields that you have.

Basically the SQL for the first append will look like:
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO [Table Name] (Serial1, AlphaAlias1)
  2. SELECT Serial, AlphaAlias
  3. FROM [Original Table]
  4. WHERE ([ID] Mod 4000 Between 1 And 1000);
The next 2 fields will be Mod 4000 Between 1001 And 2000.
Then 2001 And 3000.
And the last will be:
Expand|Select|Wrap|Line Numbers
  1. WHERE (([ID] Mod 4000 = 0) Or ([ID] Mod 4000 Between 3001 And 3999))
The Mod operator divides the first number by the second number and returns the remainder.

So 1 - 1000, 4001 - 5000, 8001 - 9000, etc. will return the numbers 1-1000.
Mar 29 '07 #12
RonVACA
12
the first three queries work great. the last one returns records 3000 to 3998 subsequent sets are off by one more record incrementally. I tried the first format and it returned every 4000th record. I also set the Mod parameter to 4001 Between 3001 and 4000. This returns records 3000 to 3999 but the second set starts at 7001 to 7999 the third 8000 then 1102 to 1201 etc. Any idea how to remedy this?
Mar 30 '07 #13
Rabbit
12,516 Expert Mod 8TB
the first three queries work great. the last one returns records 3000 to 3998 subsequent sets are off by one more record incrementally. I tried the first format and it returned every 4000th record. I also set the Mod parameter to 4001 Between 3001 and 4000. This returns records 3000 to 3999 but the second set starts at 7001 to 7999 the third 8000 then 1102 to 1201 etc. Any idea how to remedy this?
Hmm, the only reason I see for this to happen is if some of the numbers in the id are being skipped. Check the numbers and see if that's the case.
Mar 30 '07 #14
RonVACA
12
I thougth that might be the case as well but they are all there.
Mar 30 '07 #15
Rabbit
12,516 Expert Mod 8TB
Hmm I'm stumped, I just tested this with 20,000 records and it works just fine.

Let's see if someone else can figure out why it's doing this. As far as I can tell, the theory is sound and it worked for the 20,000 records I tested.
Mar 30 '07 #16
Rabbit
12,516 Expert Mod 8TB
I also noticed that the records don't get sorted by the ID so perhaps the records are all there but just in a different place? Sort by ID and see if that's the case.
Mar 30 '07 #17
Denburt
1,356 Expert 1GB
For me I think the easiest way would be to create a function with a loop and let it run.
Expand|Select|Wrap|Line Numbers
  1. Cnt=1
  2. Cnt1=1000
  3. Do until rs.eof
  4. INSERT INTO [Table Name] (Serial1, AlphaAlias1)
  5. SELECT Serial, AlphaAlias
  6. FROM [Original Table]
  7. WHERE ([ID] Between " & Cnt & " And " &  Cnt1);
  8. Cnt = Cnt1 + 1000
  9. Cnt1 = Cnt+cnt1
  10. Loop
  11.  
This code is incomplete and not tested.
It may need some toying with but something like this should do the trick. if you need any help with the code let me know (you didn't state wether you had any VBA abilities).
Mar 30 '07 #18
RonVACA
12
I also noticed that the records don't get sorted by the ID so perhaps the records are all there but just in a different place? Sort by ID and see if that's the case.
The data is sorted on the ID field, and there are no gaps in the incremental listing.
Mar 30 '07 #19
RonVACA
12
For me I think the easiest way would be to create a function with a loop and let it run.
Expand|Select|Wrap|Line Numbers
  1. Cnt=1
  2. Cnt1=1000
  3. Do until rs.eof
  4. INSERT INTO [Table Name] (Serial1, AlphaAlias1)
  5. SELECT Serial, AlphaAlias
  6. FROM [Original Table]
  7. WHERE ([ID] Between " & Cnt & " And " &  Cnt1);
  8. Cnt = Cnt1 + 1000
  9. Cnt1 = Cnt+cnt1
  10. Loop
  11.  
This code is incomplete and not tested.
It may need some toying with but something like this should do the trick. if you need any help with the code let me know (you didn't state wether you had any VBA abilities).
This is what i was originally thinking of doing but did not know how to write it. I have very limited VB skills but lets see if I get what to do.
I created the following function:
Public Function ParseData()
Cnt = 1
Cnt1 = 1000
Do Until rs.EOF
SELECT AANAAANA.Serial_Number, AANAAANA.Alph_Alias INTO printdatatest
FROM AANAAANA
WHERE ([ID] Between " & Cnt & " And " & Cnt1)
Cnt = Cnt + 1000
Cnt1 = Cnt + Cnt1
Loop
End Function
and then call the function in a macro in Access. when i do i get a parse error on the select and where statements. Am I missing something.
Mar 30 '07 #20
Denburt
1,356 Expert 1GB
This is what i was originally thinking of doing but did not know how to write it. I have very limited VB skills but lets see if I get what to do.
I created the following function:
Expand|Select|Wrap|Line Numbers
  1. Public Function ParseData()
  2. Cnt = 1
  3. Cnt1 = 1000
  4. Do Until rs.EOF
  5. SELECT AANAAANA.Serial_Number, AANAAANA.Alph_Alias INTO printdatatest
  6. FROM AANAAANA
  7. WHERE ([ID] Between " & Cnt & " And " & Cnt1)
  8. Cnt = Cnt + 1000
  9. Cnt1 = Cnt + Cnt1
  10. Loop
  11. End Function
and then call the function in a macro in Access. when i do i get a parse error on the select and where statements. Am I missing something.
Your on the right track.
This one is a little better and more complete:
Expand|Select|Wrap|Line Numbers
  1. 'Use a sub unless you want to return recors to the procedure that calls this one
  2. Public Sub ParseData()
  3. 'Declare your variables
  4. Dim Cnt As Long
  5. Dim Cnt1 As Long
  6. Dim DB As Database
  7. Dim rs As Recordset
  8. Set DB = CurrentDb
  9. Dim MaxNumb As Long
  10. Cnt = 1
  11. Cnt1 = 1000
  12. Set rs = DB.OpenRecordset("SELECT Id as MaxofID FROM [Original Table] ")
  13. If Not rs.EOF Then
  14. rs.MoveFirst
  15. MaxNumb = rs!MaxOfID
  16. Do Until Cnt1 >= MaxNumb
  17. DoCmd.RunSQL ("INSERT INTO [Table Name] (Serial1, AlphaAlias1) SELECT Serial, AlphaAlias FROM [Original Table] WHERE ([ID] Between " & Cnt & " And " & Cnt1 & ")")
  18.  
  19. Cnt = Cnt + 1000
  20. Cnt1 = Cnt + Cnt1
  21. Loop
  22. End If
  23. 'Close your recorsets or they will continue to consume resources and make your database expand way more than it should.
  24. rs.Close
  25. Set rs = Nothing
  26. Set DB = Nothing
  27. End Sub
  28.  
Does that give you a little more understanding?
Mar 30 '07 #21
RonVACA
12
Your on the right track.
This one is a little better and more complete:
Expand|Select|Wrap|Line Numbers
  1. 'Use a sub unless you want to return recors to the procedure that calls this one
  2. Public Sub ParseData()
  3. 'Declare your variables
  4. Dim Cnt As Long
  5. Dim Cnt1 As Long
  6. Dim DB As Database
  7. Dim rs As Recordset
  8. Set DB = CurrentDb
  9. Dim MaxNumb As Long
  10. Cnt = 1
  11. Cnt1 = 1000
  12. Set rs = DB.OpenRecordset("SELECT Id as MaxofID FROM [Original Table] ")
  13. If Not rs.EOF Then
  14. rs.MoveFirst
  15. MaxNumb = rs!MaxOfID
  16. Do Until Cnt1 >= MaxNumb
  17. DoCmd.RunSQL ("INSERT INTO [Table Name] (Serial1, AlphaAlias1) SELECT Serial, AlphaAlias FROM [Original Table] WHERE ([ID] Between " & Cnt & " And " & Cnt1 & ")")
  18.  
  19. Cnt = Cnt + 1000
  20. Cnt1 = Cnt + Cnt1
  21. Loop
  22. End If
  23. 'Close your recorsets or they will continue to consume resources and make your database expand way more than it should.
  24. rs.Close
  25. Set rs = Nothing
  26. Set DB = Nothing
  27. End Sub
  28.  
Does that give you a little more understanding?

how do i call a sub from Access. i see how to call a function.
Mar 30 '07 #22
Denburt
1,356 Expert 1GB
You got me there maybe Function would be better for your needs. I never use Macros so.... I didn't realize that there would be such a delima. :)
Mar 30 '07 #23
RonVACA
12
is there a better way to run the sub than to use a macro?
Mar 30 '07 #24
Denburt
1,356 Expert 1GB
It really depends on what you are using it for. Most of my commands are run from a button on a form or something to that effect. Like the Main Menu for Example, I have a db that needs the contracts to rollover when they get close to expiring so I placed a button on the main menu so it would be easy for them to open update close out and move on.

If this is just a one time shot then you can always place your code in a standalone module and place your pointer in the VBA sub or function and press F5 and it should run.
Mar 30 '07 #25

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

Similar topics

3
by: Brian Oster | last post by:
After applying security patch MS03-031 (Sql server ver 8.00.818) a query that used to execute in under 2 seconds, now takes over 8 Minutes to complete. Any ideas on what the heck might be going...
2
by: Laphan | last post by:
Hi All Sorry for the spare info. It wasn't because I didn't want to display it. I just didn't know how to get the data that you needed. I think I've got it now so please note the following: ...
4
by: news-server.tampabay.rr.com | last post by:
Hi, Below is a stock script I found which controls a framed environment. My problem is that if a URL has a query string attached, that string does not pass through. Can someone please let me...
7
by: Melissa | last post by:
I'm trying to create a function that I can put in a query field that will consecutively number the records returned by the query starting at 1 and will start at 1 each time the query is run. So far...
2
by: Betrock | last post by:
This is probably very simple, but I just can't see my way thru it..... Short version: keyed values(numeric)in a lookup table are stored in a main table. They are displayed as text values - the...
12
by: Joe Stanton | last post by:
Hello Group I have a query that works in Oracle and SQL Server, but fails in Microsoft Access. The query is: SELECT data fromTABLE1 WHERE data>='A&' AND data<'A''' Here is my sample data:
6
by: Jack Orenstein | last post by:
Suppose I have a table as follows: testdb=> \d person Table "public.person" Column | Type | Modifiers ------------+-------------------------+----------- id |...
3
by: cover | last post by:
I have a table with 50 fields that receive input depending on whether that input came in from a 'shaker' form or a 'conveyor' form. Input from the 'conveyor' form might populate 25 fields while...
1
by: Oliver Marshall | last post by:
Hi, Im after a simple script to help sort my downloads. Basically I have a downloads folder, and at the moment I have directory browsing enabled so that i can download files. What I want is...
0
by: Chuck36963 | last post by:
Hi all, I've been working on a listing problem and I can't figure out how to work it out. I have looked far and wide on the web to find answers, but I'd like other peoples input on my project in...
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: 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:
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.