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

Find gaps in a sequence and insert rows

Hi all

Using A2K, I have a table which lists account numbers and details for
12 periods (a year's worth) for each account:

A/C # Period
============
1001 1
1001 2
1001 3

For any account, any particular period may be missing (so there may be
no row for, say, period 10 for Account 1001). Is there any way I can
loop through the table and create records for the missing periods?

Thanks in advance

May 23 '06 #1
4 5761
On 22 May 2006 17:05:36 -0700, "Cate"
<ca***************@chapmantripp.com> wrote:

Use some VBA code to loop over the records in this table, and add what
was not found. Keywords: OpenRecordset, MoveNext, AddNew, Update.

-Tom.
Hi all

Using A2K, I have a table which lists account numbers and details for
12 periods (a year's worth) for each account:

A/C # Period
============
1001 1
1001 2
1001 3

For any account, any particular period may be missing (so there may be
no row for, say, period 10 for Account 1001). Is there any way I can
loop through the table and create records for the missing periods?

Thanks in advance


May 23 '06 #2

For emphasis I am going to shout the next bit

AS THE FOLLOWING WILL MAKE CHANGES TO YOUR DATA WHICH CANNOT BE UNDONE TEST
THIS ON A COPY OF YOUR DATABASE FIRST AND BACKUP YOUR DATABASE BEFORE MAKING
THE FINAL RUN ON YOUR LIVE DATA

You can do this with a single query statement.

You need to create a table which just holds the period numbers (called
Periods in the example below) with a single field (called period in the
example below) you then fill the table with each period e.g.

Period
1
2
3
4
5
6
7
8
9
10
11
12

In the example below it is assumed that the table you quote is called
PeriodsAccount with the two field names you quote.

Create a new query
Change to SQL view
Paste in the following SQL

INSERT INTO PeriodsAccount ( [A/C #], Period )
SELECT a.[A/C #] , a.Period
FROM
(SELECT DISTINCT PeriodsAccount.[A/C #], Periods.Period
FROM Periods, PeriodsAccount) as a
Left Join PeriodsAccount as b
ON a.[A/C #] = b.[A/C #]
AND a.Period = b.Period
WHERE b.[A/C #] Is Null

Edit the SQL for table and field names.
Check the output by switching to datasheet view
Run the query.

and again for emphasis
AS THE ABOVE WILL MAKE CHANGES TO YOUR DATA WHICH CANNOT BE UNDONE TEST THIS
ON A COPY OF YOUR DATABASE FIRST AND BACKUP YOUR DATABASE BEFORE MAKING THE
FINAL RUN ON YOUR LIVE DATA.
--

Terry Kreft
"Cate" <ca***************@chapmantripp.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
Hi all

Using A2K, I have a table which lists account numbers and details for
12 periods (a year's worth) for each account:

A/C # Period
============
1001 1
1001 2
1001 3

For any account, any particular period may be missing (so there may be
no row for, say, period 10 for Account 1001). Is there any way I can
loop through the table and create records for the missing periods?

Thanks in advance

May 23 '06 #3
Yet another way:

Use a crosstab query, using Period as the column heading, A/C# as the row
heading, and a count of either column as the Value. Assuming that you have
at least one row with each period anywhere in the table, you could look for
null values in the period columns.

Strictly speaking Terry Kreft's answer is the better one. If you know the
periods in advance, then run a setup process at some point or periodically
to populate a table with all the account/period combinations, then use a
third column to contain (or not contain) a value, then test that column.

But the crosstab can be useful and requires no change to the table structure
and use. It's main deficiency is that you must do something to guarantee
that you get all the columns you require, and even that's pretty easy in
many cases. For example:

TRANSFORM Count(AccountsByPeriod.ID) AS CountOfID
SELECT AccountsByPeriod.Account
FROM AccountsByPeriod
GROUP BY AccountsByPeriod.Account
PIVOT AccountsByPeriod.Period in (1, 2, 3, 4);

Note the expression "in (1, 2, 3, 4)". In my example, I am assuming only 4
periods. The "in" expression guarantees that all four columns appear,
regardless of data present in the table. Yours would extend, presumably, to
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) if the periods are months. As long
as you know ahead of time all the possible periods, the crosstab could do
the whole job.
May 23 '06 #4
Thanks for all the help - appreciated.

May 24 '06 #5

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

Similar topics

4
by: SQLJunkie | last post by:
Here is an issue that has me stumped for the past few days. I have a table called MerchTran. Among various columns, the relevant columns for this issue are: FileDate datetime , SourceTable...
6
by: christopher.secord | last post by:
I have a table containing typed log entries. One log entry is supposed to be created every twelve hours, but sometimes there are gaps. I need to create a report showing the time of entry, and the...
5
by: Glenn | last post by:
Hi! Server info - Win2K3 Server +SP1 with 1 GB Memory and 1.5 GB Virtual Memory SQL Server 2000 Enterprise Edition + SP3 running on this. Required result - Create a SQL Script that will...
108
by: Bryan Olson | last post by:
The Python slice type has one method 'indices', and reportedly: This method takes a single integer argument /length/ and computes information about the extended slice that the slice object would...
3
by: Robert McGregor | last post by:
Hi there, I was wondering if anyone could help with this problem. I have a table with about 250,000 rows that relate to files that have been processed elsewhere in our business. Each file has...
17
by: glenn.robinson | last post by:
Hello, DB2 for iSeries - V5R2 I have a table with a non-unique index, column name SEDOL. I need to extract the 2nd row from each set of SEDOL rows in the table. If only one SEDOL row exists...
4
by: Szymon Dembek | last post by:
Hi Recently I did some DB2 and ODBC coding in Visual FoxPro. I bumped on a problem I cannot resolve. When I issue a delete statement that deletes no rows (no rows qualify for the WHERE...
3
by: markcash | last post by:
I have a web application where I am displaying the current inmate population for the county jail. Part of the information that I display is the mugshot. Unfortunately, they have changed the way...
2
by: jaka | last post by:
Hi, I'm in need of some expert help in sorting a numerical/alphanumerical employee ID field (5&6 characters in length). Also seperating the alphanumerical IDs and finding the gaps in the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.