Skip to content
Advertisement

How to count number of non-consecutive values in a column using SQL?

Following-up on my question here. Say I have a table in an Oracle database like the one below (table_1) which tracks service involvement for a particular individual:

My goal is to get a summary table which lists, for all unique individuals, whether there was service involvement and the number of distinct service episodes (in this case 2 for bill and 3 for susy), where a distinct service episode is identified by a break in activity over days.

To get any service involvement, I would use the following query

However, I’m stuck as to how I would get the number of service involvements (2). Using a static dataframe in R, you would use run length encoding (see my original question), but I don’t know how I could accomplish this in SQL. This operation would be run over a large number of records so it would be impractical to store the entire data frame as an object and then run it in R.

Edit: My expect output would be as follows:

Thanks for any help!

Advertisement

Answer

I would suggest using lag(). The idea is to count a “1”, but only when the preceding value is zero or null:

You can simplify this a little by using a default value for lag():

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement