How to display values from the range

  • Thread starter Thread starter tzmy79
  • Start date Start date
T

tzmy79

Hello.
4 example
There is the table, each record of this table has two values named
"from" and "to".

The goal is to write the query which displays values from 1 to 255
without values from the ranges from the table.

Example
Range1:
From= 5
To= 10
Range2:
From= 15
To= 254

The result will looks like:

1,2,3,4,11,12,13,14,255

Thank you very much in advance
Best regards

Tomasz
 
Hi Tomasz,

if you have a table with 255 records numbered from 1 to 255
try it
----
SELECT tblNumbers.Number
FROM tblNumbers LEFT JOIN tblRanges
ON tblNumbers.Number>=tblRanges.From
AND tblNumbers.Number<=tblRanges.To
WHERE tblRanges.From IS NULL
ORDER BY tblNumbers.Number
----

if you don't want to have the table write this query
----
SELECT U.Nr+D.Nr+C.Nr AS [Number]
FROM (
SELECT TOP 1 0 AS Nr
FROM MSysObjects
UNION ALL
SELECT TOP 1 1
FROM MSysObjects
UNION ALL
SELECT TOP 1 2
FROM MSysObjects
UNION ALL
SELECT TOP 1 3
FROM MSysObjects
UNION ALL
SELECT TOP 1 4
FROM MSysObjects
UNION ALL
SELECT TOP 1 5
FROM MSysObjects
UNION ALL
SELECT TOP 1 6
FROM MSysObjects
UNION ALL
SELECT TOP 1 7
FROM MSysObjects
UNION ALL
SELECT TOP 1 8
FROM MSysObjects
UNION ALL
SELECT TOP 1 9
FROM MSysObjects) AS U,
(SELECT TOP 1 10 AS Nr
FROM MSysObjects
UNION ALL
SELECT TOP 1 0
FROM MSysObjects
UNION ALL
SELECT TOP 1 20
FROM MSysObjects
UNION ALL
SELECT TOP 1 30
FROM MSysObjects
UNION ALL
SELECT TOP 1 40
FROM MSysObjects
UNION ALL
SELECT TOP 1 50
FROM MSysObjects
UNION ALL
SELECT TOP 1 60
FROM MSysObjects
UNION ALL
SELECT TOP 1 70
FROM MSysObjects
UNION ALL
SELECT TOP 1 80
FROM MSysObjects
UNION ALL
SELECT TOP 1 90
FROM MSysObjects) AS D,
(
SELECT TOP 1 0 AS Nr
FROM MSysObjects
UNION ALL
SELECT TOP 1 100
FROM MSysObjects
UNION ALL
SELECT TOP 1 200
FROM MSysObjects
) AS C
WHERE U.Nr+D.Nr+C.Nr Between 1 AND 255
----
save the query with name qryNumbers

then write the final query
----
SELECT qryNumbers.Number
FROM qryNumbers LEFT JOIN tblRanges
ON qryNumbers.Number>=tblRanges.From
AND qryNumbers.Number<=tblRanges.To
WHERE tblRanges.From IS NULL
ORDER BY qryNumbers.Number
 
Thank you
I will try tomorrow.

Best regards
Tomasz

Hi Tomasz,

if you have a table with 255 records numbered from 1 to 255
try it
----
SELECT tblNumbers.Number
FROM tblNumbers LEFT JOIN tblRanges
ON tblNumbers.Number>=tblRanges.From
AND tblNumbers.Number<=tblRanges.To
WHERE tblRanges.From IS NULL
ORDER BY tblNumbers.Number
----

if you don't want to have the table write this query
----
SELECT U.Nr+D.Nr+C.Nr AS [Number]
FROM (
SELECT TOP 1 0 AS Nr
FROM MSysObjects
UNION ALL
SELECT TOP 1 1
FROM MSysObjects
UNION ALL
SELECT TOP 1 2
FROM MSysObjects
UNION ALL
SELECT TOP 1 3
FROM MSysObjects
UNION ALL
SELECT TOP 1 4
FROM MSysObjects
UNION ALL
SELECT TOP 1 5
FROM MSysObjects
UNION ALL
SELECT TOP 1 6
FROM MSysObjects
UNION ALL
SELECT TOP 1 7
FROM MSysObjects
UNION ALL
SELECT TOP 1 8
FROM MSysObjects
UNION ALL
SELECT TOP 1 9
FROM MSysObjects) AS U,
(SELECT TOP 1 10 AS Nr
FROM MSysObjects
UNION ALL
SELECT TOP 1 0
FROM MSysObjects
UNION ALL
SELECT TOP 1 20
FROM MSysObjects
UNION ALL
SELECT TOP 1 30
FROM MSysObjects
UNION ALL
SELECT TOP 1 40
FROM MSysObjects
UNION ALL
SELECT TOP 1 50
FROM MSysObjects
UNION ALL
SELECT TOP 1 60
FROM MSysObjects
UNION ALL
SELECT TOP 1 70
FROM MSysObjects
UNION ALL
SELECT TOP 1 80
FROM MSysObjects
UNION ALL
SELECT TOP 1 90
FROM MSysObjects) AS D,
(
SELECT TOP 1 0 AS Nr
FROM MSysObjects
UNION ALL
SELECT TOP 1 100
FROM MSysObjects
UNION ALL
SELECT TOP 1 200
FROM MSysObjects
) AS C
WHERE U.Nr+D.Nr+C.Nr Between 1 AND 255
----
save the query with name qryNumbers

then write the final query
----
SELECT qryNumbers.Number
FROM qryNumbers LEFT JOIN tblRanges
ON qryNumbers.Number>=tblRanges.From
AND qryNumbers.Number<=tblRanges.To
WHERE tblRanges.From IS NULL
ORDER BY qryNumbers.Number
----

bye
--
Giorgio Rancati
[Office Access MVP]

Hello.
4 example
There is the table, each record of this table has two values named
"from" and "to".
The goal is to write the query which displays values from 1 to 255
without values from the ranges from the table.
Example
Range1:
From= 5
To= 10
Range2:
From= 15
To= 254
The result will looks like:

Thank you very much in advance
Best regards
 

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

Back
Top