Solved

How to check sequence missing in postgress

Posted on 2014-12-03
9
151 Views
Last Modified: 2014-12-22
Hi,

I have a table in PostgreSQL that the datatype of the field is (character varying(255) for some reason now I need to check this across all records if there is any sequence is missing, if any sequence is missing what are those sequences..

sample data

12345
12346
12347
12348
12350
12351
12356

Open in new window


 and what I want is
12349
12352
12353
12354
12355

Open in new window


and PostgreSQL version is 9.3 regards
0
Comment
Question by:hi4ppl
9 Comments
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 40479665
This solution determines which key (myfield) has missing sequential value(s) after it, and how many are missing after that key:
create table mytable
( myfield int
 );

insert into mytable values (12345);
insert into mytable values (12346);
insert into mytable values (12347);
insert into mytable values (12348);
insert into mytable values (12350);
insert into mytable values (12351);
insert into mytable values (12356);

WITH sortedData as
(	SELECT	row_number() OVER (ORDER BY myfield) as rn
	,	myfield
	FROM	mytable
)
SELECT	a.myfield
,	b.myfield - a.myfield - 1 as missing_keys
FROM	sortedData a
JOIN	sortedData b	ON	a.rn = b.rn - 1
WHERE	b.myfield - a.myfield > 1

MYFIELD	MISSING_KEYS
------- ------------
12348	1
12351	4

Open in new window

0
 
LVL 1

Author Comment

by:hi4ppl
ID: 40480437
Hi,

thanks, but my data field is character and this didn't work for that, can you suggest how to convert that and put it in this query?

regards
0
 
LVL 31

Expert Comment

by:awking00
ID: 40480606
Determine the lowest and highest numeric values of your field -
select min(num) as start, max(num) as stop from
(select to_number(yourfield,'99999') as num  ==> using format mask to accommodate highest value
 from yourtable)

Then
select * from generate_series(start,stop)  ==> substituting values for start and stop from previous query
except
select to_number(yourfield,'99999') from yourtable
0
 
LVL 24

Expert Comment

by:Tomas Helgi Johannsson
ID: 40486357
Hi!

Try this query

WITH seq_max AS (
SELECT max(yourvarcharfield) FROM yourtable
),
seq_min AS (
SELECT min(yourvarcharfield) FROM yourtable
)
SELECT * FROM generate_series((SELECT min FROM seq_min),(SELECT max FROM seq_max))
EXCEPT
SELECT yourvarcharfield FROM yourtable

Open in new window

Note that this query can be performance intensive on large tables.

Regards,
      Tomas Helgi
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 40487792
Same solution as I posted previously, this time the key-field is varchar(255) instead of integer:
create table mytable
( myfield varchar(255)
 );

insert into mytable values ('12345');
insert into mytable values ('12346');
insert into mytable values ('12347');
insert into mytable values ('12348');
insert into mytable values ('12350');
insert into mytable values ('12351');
insert into mytable values ('12356');

WITH sortedData as
(	SELECT	row_number() OVER (ORDER BY myfield) as rn
	,	CAST(myfield as int) as myfield
	FROM	mytable
)
SELECT	a.myfield
,	b.myfield - a.myfield - 1 as missing_keys
FROM	sortedData a
JOIN	sortedData b	ON	a.rn = b.rn - 1
WHERE	b.myfield - a.myfield > 1

MYFIELD	MISSING_KEYS
------- ------------
12348	1
12351	4

Open in new window

0
 
LVL 31

Expert Comment

by:awking00
ID: 40489139
The asker seems to want the missing values in the series, not the number of missing values. The following should return the missing values as character date:
with cte as
(select min(to_number(yourfield,'99999')) as start, max(to_number(yourfield,'99999')) as stop
 from yourtable)
select to_char(g.num,'99999')
from generate_series(cte.start,cte.stop) as g(num)
left join yourtable y on y.yourfield = to_char(g.num,'99999')
where y.yourfield is null;
0
 
LVL 31

Expert Comment

by:awking00
ID: 40489143
typo - as character data (not date)
0
 
LVL 22

Expert Comment

by:earth man2
ID: 40507154
select generate_series( col_name::integer + 1, alias_1::integer - 1 ) from
( select col_name,
lead(col_name) over( order by col_name) as alias_1
from tt ) as foo
where alias_1 <> (col_name::integer+1)::text;

 generate_series
-----------------
           12349
           12352
           12353
           12354
           12355
(5 rows)
0
 
LVL 31

Expert Comment

by:awking00
ID: 40513034
and what I want is

12349
12352
12353
12354
12355
                                  1:
and what accepted solution provides is
MYFIELD      MISSING_KEYS
------- ------------
12348      1
12351      4

???
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now