-1
I’m trying to build a select that will locate the missing items from a database. This is super unusual, 'select' the items that don’t exist. Let’s say I have a table called TBL795 with the column NRBEM that should, in theory, not have gaps in the sequence of registered items.
It must be so:
The numbers registered are in sequence.
If so:
this wrong, because some items were not registered.
In a table with thousands of items it is difficult to check by select whether an item is missing and which items are missing.
One of the ways would be to create a TEST table:
CREATE TABLE TESTE (
NRBEM VARCHAR(15))
feed it with the command:
INSERT INTO TESTE
WITH RECURSIVE
cnt(NRBEM) AS (VALUES(1) UNION ALL SELECT NRBEM+1 FROM cnt WHERE NRBEM <100000)
SELECT NRBEM FROM cnt A
and run select
SELECT A.NRBEM FROM TESTE A LEFT JOIN TBL795 B
ON A.NRBEM = B.NRBEM
WHERE B.NRBEM IS NULL
i see all the items that are missing in my table.
Since the command:
WITH RECURSIVE
cnt(NRBEM) AS (VALUES(1) UNION ALL SELECT NRBEM+1 FROM cnt WHERE NRBEM <100000)
SELECT NRBEM FROM cnt
creates a virtual table in memory I would like to make the following select:
SELECT NRBEM FROM (
WITH RECURSIVE
cnt(NRBEM) AS (VALUES(1) UNION ALL SELECT NRBEM+1 FROM cnt WHERE NRBEM <100000)
SELECT NRBEM FROM cnt ) A LEFT JOIN TBL795 B
ON A.NRBEM = B.NRBEM
But it doesn’t work.
Thus
SELECT X FROM (
WITH RECURSIVE
cnt(X) AS (VALUES(1) UNION ALL SELECT X+1 FROM cnt WHERE X <100000)
SELECT X FROM cnt ) A LEFT JOIN TBL795 B
ON A.X = B.NRBEM
ALMOST works, that is, there is no error. But doesn’t bring missing items, brings all data from the virtual table.
Summary: there is no way I can make this select with this virtual 'table' without having to physically create it and feed it?
I remember doing something like that, but now I don’t know where that select. :(
I posted the question, even though I found the answer a little earlier. Thinking about helping someone and I get negative votes. Frankly.
– Reginaldo Rigo
I wasn’t the one who said no, but I think your question was a little confusing. Help is nice, but for that you could make a little more clear what you want. Furthermore, the proposed solution depends a little on faith, as it has no explanation of how it works. I am a little afraid to comment on these things, because sometimes it is poorly received, but I think would be points that if improved, could value the two posts.
– Bacco