SQL display records that do not exist between a range

Hi All,
I'm trying to display a list of payment Id's from my table that do not exist between a range.

I have this query which shows me what payments exist in a range


select PaymentID from payments where paymentID >000001 and PaymentId < 100000


but I want to display the delta.
ie. The data that does not exist between that range.

Lets say for arguments sake I have 5 records in there
000035
000102
000120
000155
000174

I want to display the 99995 records that dont exist.

How can one do this?

Thanks



doyle007Asked:
Who is Participating?
 
Kent OlsenData Warehouse Architect / DBACommented:

Hi Doyle,

You should be able to generate a list of IDs with the following:

  SELECT 1 as match_id
  FROM dual
  CONNECT BY LEVEL <= 100000

Then join to it

  SELECT *
  FROM
  (
    SELECT 1 as match_id
    FROM dual
    CONNECT BY LEVEL <= 100000
  ) T0
  LEFT JOIN mytable
    ON t0.match_id = mytable.id
  WHERE mytable.id is NULL;


Kent
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Doyle,

It kind of depends on which database engine you're using.  :(

You need to read all of the keys that you want to report on, or generate them.  Then OUT ER join them to the payments.  Any joined item from payments that results in a NULL is selected.


Kent
0
 
doyle007Author Commented:
Its oracle :)

Let me see if I can work that out....
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
doyle007Author Commented:
Hmm...that just creates a table with all records as 1 ...
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Let's pick a smaller example.  :)

Try this:

 
SELECT rownum 
FROM dual 
CONNECT BY LEVEL <= 5;

Open in new window

0
 
doyle007Author Commented:
This works! genius. Thanks Kdo.


SELECT rownum as match_id
  FROM dual
  CONNECT BY LEVEL <= 100000;

  SELECT *
  FROM
  (
    SELECT rownum as match_id
    FROM dual
    CONNECT BY LEVEL <= 100000
  ) T0
  LEFT JOIN masterdeid
    ON t0.match_id = masterdeid.deid
  WHERE masterdeid.deid is NULL;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.