| re: Find gaps in a sequence and insert rows
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. |