Skip to Main Content
  • Questions
  • How do I check data in a column to make sure the numbers are incrementing.

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Jeff.

Asked: April 28, 2008 - 9:45 am UTC

Last updated: April 28, 2008 - 3:31 pm UTC

Version: 8.1.7.4

Viewed 1000+ times

You Asked

I have a table of data that is sorted by a date field. When sorted I have another column with a number. This number must show either the same as the previous value (as sorted by date) or increment. If the number goes down, it is an error in the data entry and needs to be checked.

I need a query that will sort the data by the date column, then check this number field to make sure the numbers are either the same or incrementing. A query that will show just the offending rows is also an option. I just don't know where to start. I can sort the columns, but don't know how to make this exclusion. Please advise. Thanks

and Tom said...

select ...
 from (
select a.*,  
       lag(num_col) over (order by dt_column) last_num,
       lead(num_col) over (order by dt_column)next_num
  from table a
)
where <whatever you want...>
order by dt_column



for example, you could

where next_num < num_col <<<=== bad row




Is this answer out of date? If it is, please let us know via a Comment

More to Explore

Analytics

Analytic SQL got you confused? Check out Connor McDonald's complete video course.