I want to find rows missing from a sequence of values in my table with differents formats. For an example, look at this.
I want to find the missing lines via c_order_id and documentno. the one with '_p' at the end, and the other without '_p', just the numbers.
CREATE TABLE documents (
"order_id" VARCHAR(22),
"documentno" VARCHAR(20)
);
INSERT INTO documents
("order_id", "documentno")
VALUES
('100001120', 'PROFS/2021/02050_P'),
('100001125' ,'PROFS/2021/02055_p'),
('100001127', 'PROFS/2021/02056'),
('100001135' ,'PROFS/2021/02060'),
('100001139' ,'AGB/2021/02040'),
('100001172' ,'AGB/2021/02047')
Query used:
WITH cte as (
SELECT LEFT("documentno",12) lpart , MIN(RIGHT("documentno",5)) minpart, MaX(RIGHT("documentno",5)) maxpart FROM documents
groUP BY LEFT("documentno",12))
sELECT lpart || generate_series(minpart::INTEGER, maxpart::INTEGER) as missing_documentno FROM cte
except
select documentno from documents
ORDER bY missing_documentno;
This query generates
ERROR: invalid input syntax for type integer: "055_p"