query grouping by fixed interval

S

Stuartm

I would like to create a query which will allow me to group by a fixed
interval for selected table i.e. a record from table of road data has a name
of "abc" with a start of 0m and end of 490m, i would like the following

abc Raod 0-100m
abc Raod 101m -200m
..
..
abc road 401 -490m

Can antyone help me?
 
D

Dale Fye

Stuart,

I'm assuming you want these results in a query.

In cases like this, I have a table (tblNumbers) which contains a single
field (lngNumber) with values 0 through 9.

I then create a query (qryNumbers) based on this table to generate numbers
in the range I need. For example, a query with numbers from 0 to 999 would
look like:

SELECT Hundreds.lngNumber * 100 + Tens.lngNumber * 10 + Ones.lngNumber as
lngNumber
FROM tblNumbers as Hundreds, tblNumbers as Tens, tblNumbers as Ones

Now, to anwer your question. I created a table (tbl_Roads) and entered
values for fields RoadName, Start and End (keep in mind that these are
numeric values in line with your 0 and 490). I then created the following
query:

SELECT tbl_Roads.RoadName,
IIf([tbl_Roads].[Start]<=[lngNumber]*[Interval],[lngNumber]*[Interval],[tbl_Roads].[Start])-([lngNumber]*[Interval]>[tbl_Roads].[Start])
AS Start,
IIf(([lngNumber]+1)*[Interval]<[tbl_Roads].[End],([lngNumber]+1)*[Interval],[tbl_Roads].[End]) AS [End]
FROM tbl_Roads, qryNumbers
WHERE
(((IIf([tbl_Roads].[Start]<=[lngNumber]*[Interval],[lngNumber]*[Interval],[tbl_Roads].[Start])-([lngNumber]*[Interval]>[tbl_Roads].[Start]))<[tbl_Roads].[End])
AND
((IIf(([lngNumber]+1)*[Interval]<[tbl_Roads].[End],([lngNumber]+1)*[Interval],[tbl_Roads].[End]))>[tbl_Roads].[Start]))
ORDER BY tbl_Roads.RoadName,
IIf([tbl_Roads].[Start]<=[lngNumber]*[Interval],[lngNumber]*[Interval],[tbl_Roads].[Start])-([lngNumber]*[Interval]>[tbl_Roads].[Start]);

When you run this query, it asks you for an interval (this can be any
positive number > 0). It then uses the values of the [lngNumber] field in
qryNumbers as multipliers of the [Interval] value you entered to generate
multiple records based on the Start and End values in the table for each
record. BTW, these values do not need to begin or end on a number that is
divisible by the [Interval] value.

The only thing you need to be sure of is that the highest number in
[qryNumbers] times the [Interval] is greater than the highest number in your
[End] field.
 
S

Stuartm

Dale,

Thanks for your help on this, works a perfectly, much appreciated.

Regards

Stu

Dale Fye said:
Stuart,

I'm assuming you want these results in a query.

In cases like this, I have a table (tblNumbers) which contains a single
field (lngNumber) with values 0 through 9.

I then create a query (qryNumbers) based on this table to generate numbers
in the range I need. For example, a query with numbers from 0 to 999 would
look like:

SELECT Hundreds.lngNumber * 100 + Tens.lngNumber * 10 + Ones.lngNumber as
lngNumber
FROM tblNumbers as Hundreds, tblNumbers as Tens, tblNumbers as Ones

Now, to anwer your question. I created a table (tbl_Roads) and entered
values for fields RoadName, Start and End (keep in mind that these are
numeric values in line with your 0 and 490). I then created the following
query:

SELECT tbl_Roads.RoadName,
IIf([tbl_Roads].[Start]<=[lngNumber]*[Interval],[lngNumber]*[Interval],[tbl_Roads].[Start])-([lngNumber]*[Interval]>[tbl_Roads].[Start])
AS Start,
IIf(([lngNumber]+1)*[Interval]<[tbl_Roads].[End],([lngNumber]+1)*[Interval],[tbl_Roads].[End]) AS [End]
FROM tbl_Roads, qryNumbers
WHERE
(((IIf([tbl_Roads].[Start]<=[lngNumber]*[Interval],[lngNumber]*[Interval],[tbl_Roads].[Start])-([lngNumber]*[Interval]>[tbl_Roads].[Start]))<[tbl_Roads].[End])
AND
((IIf(([lngNumber]+1)*[Interval]<[tbl_Roads].[End],([lngNumber]+1)*[Interval],[tbl_Roads].[End]))>[tbl_Roads].[Start]))
ORDER BY tbl_Roads.RoadName,
IIf([tbl_Roads].[Start]<=[lngNumber]*[Interval],[lngNumber]*[Interval],[tbl_Roads].[Start])-([lngNumber]*[Interval]>[tbl_Roads].[Start]);

When you run this query, it asks you for an interval (this can be any
positive number > 0). It then uses the values of the [lngNumber] field in
qryNumbers as multipliers of the [Interval] value you entered to generate
multiple records based on the Start and End values in the table for each
record. BTW, these values do not need to begin or end on a number that is
divisible by the [Interval] value.

The only thing you need to be sure of is that the highest number in
[qryNumbers] times the [Interval] is greater than the highest number in your
[End] field.

----
HTH
Dale



Stuartm said:
I would like to create a query which will allow me to group by a fixed
interval for selected table i.e. a record from table of road data has a name
of "abc" with a start of 0m and end of 490m, i would like the following

abc Raod 0-100m
abc Raod 101m -200m
.
.
abc road 401 -490m

Can antyone help me?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top