Q
Problem solve Get help with specific problems with your technologies, process and projects.

# N consecutive rows, Part 1 of 4

We need to know when a service code has been used for a person for N consecutive months. The day part of the date...

may not be the same from month to month (eg. 7/1 and 8/5), so we're not sure if DateAdd will work. Also the N part of the N consecutive months is different for each service code (e.g. some are every four months, some are every six months). We have a table of entries with Name, SC, and UseDate, which looks like:

```Joe F55 7/1/04 Joe F55 8/5/04
```

This would be two consecutive months. We also have a table of ServiceCodes and the number of consecutive months to look for, which looks like:

```F55 6 F50 4
```

We are working in Access, and need to make a report listing names and codes that exceed the number of consecutive months. We want to base the report on a query, but are not sure how to write the query.

Great question!

The following solution involves a theta self-join. If that sounds strange, we will break it down, take it slowly, and see how it works from the ground up.

Before getting to the SQL, let's do a quick review of joins. Consider the numbers 1 through 9. Join these numbers to themselves, and you get 9 * 9 = 81 different combinations. That's the typical cross join we're all familiar with. It's also a self-join. In fact, a self-join can be a cross join, inner join, or any other type of join. In this case, we're interested primarily in a certain type of inner join.

In the following diagram, there's an X where the value down the left is equal to the value across the top. This illustrates an equi-join:

``` 1 2 3 4 5 6 7 8 9 1 X . . . . . . . . 2 . X . . . . . . . 3 . . X . . . . . . 4 . . . X . . . . . 5 . . . . X . . . . 6 . . . . . X . . . 7 . . . . . . X . . 8 . . . . . . . X . 9 . . . . . . . . x
```

An equi-join is a special type of inner join. It uses equality of values as the join condition. The cross join, by comparison, is also a type of inner join, but it has no join condition, so every value is joined with every value, and there would be an X in every spot in that diagram.

A theta join uses some other condition, not equality, in order to join rows. In the following diagram, there's an X where the value down the left is equal to or greater than the value across the top. This "triangle" appearance illustrates a theta join:

``` 1 2 3 4 5 6 7 8 9 1 X . . . . . . . . 2 X X . . . . . . . 3 X X X . . . . . . 4 X X X X . . . . . 5 X X X X X . . . . 6 X X X X X X . . . 7 X X X X X X X . . 8 X X X X X X X X . 9 X X X X X X X X X
```

In the following diagram, there's an X where the value down the left is equal to or greater than the value across the top, but not greater than 2 greater. The triangle has been "cut off" and so this illustrates a different theta join:

``` 1 2 3 4 5 6 7 8 9 1 X . . . . . . . . 2 X X . . . . . . . 3 X X X . . . . . . 4 . X X X . . . . . 5 . . X X X . . . . 6 . . . X X X . . . 7 . . . . X X X . . 8 . . . . . X X X . 9 . . . . . . X X X
```

In general, there are a zillion ways you can construct a theta join. We'll be using a version of the theta join in the last diagram.

Please proceed to N consecutive rows, Part 2 of 4.

This was last published in September 2004

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Focus, scope and spotting opportunity are key to role of CDO

Chief data officers and experts see the CDO role as changing to a more strategic orientation -- especially finding key ...

• ### Good data quality for machine learning is an analytics must

As companies add machine learning applications, they need to really understand -- and be able to improve -- their data. That's ...

• ### Chief data officer role: Searching for consensus

The chief data officer role is about many things -- regulations, innovation, AI and more. Consultant Randy Bean discussed the ...

• ### Data analytics in government efforts lack structure

Data analytics in government agencies lack organization, focusing on immediate problems instead of attacking underlying causes ...

• ### A complete guide to buying data discovery software

Data discovery software turns data into business insights, and it's vital for GDPR compliance. This guide outlines the products ...

• ### Rising demand for business analytics education programs

Colleges and universities are increasingly offering business analytics degrees. The graduates can help build IT and business ...

## SearchSAP

• ### On-premises, hosted most popular S/4HANA deployment options

The pure cloud -- SaaS -- version of SAP's newest ERP, S/4HANA Cloud, lacks some of the same features of the on-premises version....

• ### S/4HANA public cloud version can get lost in cloud confusion

The 'true' public cloud is the streamlined SaaS version of on-premises S/4. But private cloud options are often conflated with ...

• ### SAP S/4HANA migration: What you need to know

There's a lot to consider when contemplating a move to SAP S/4HANA, and this essential guide provides a starting point, including...

## SearchSQLServer

• ### Six sample databases for SQL Server and how to find them

SQL Server sample databases are useful for test and dev, but they can be difficult to parse. Use this SQL database sample ...

• ### A quick tutorial on SQL Server maintenance plans

SQL Server maintenance plans get a bad rap, but for DBAs who need a simple way to maintain databases, Microsoft's built-in tools ...

• ### Proposed Microsoft-GitHub buy confirms open source role in cloud

The looming Microsoft-GitHub pairing confirms the company's rebirth as an open source friend. Data tools on the Azure cloud are ...

## TheServerSide.com

• ### Jenkins Git integration: GitHub pull request via the Git plugin

This Jenkins Git integration tutorial demonstrates how to create a freestyle build job that performs a Jenkins GitHub pull ...

• ### Financial firms, vendors push self-service software delivery

Self-service DevOps automation appeals to enterprises that must push out new code as they adapt to changing requirements.

• ### IT projects and software teams need to include Agile people

Not every idea deserves equal weight in a software development project, but Agile people know that garnering input from a wide ...

## SearchDataCenter

• ### Four top open source SIEM tools you should know

Open source SIEM software offers organizations a way to test capabilities and augment existing functionalities for analytics and ...

• ### Rackspace colocation program hosts users' legacy servers

Rackspace now has a managed colocation program that it hopes to upsell its customers with additional services, once their servers...

Broadcom has acquired CA Technologies in a move some believe is largely financially motivated, while others see an opportunity ...

## SearchContentManagement

• ### At OpenText Enterprise World, security and AI take center stage

OpenText unveiled its new application, OT2, at OpenText Enterprise World 2018, while also touting the importance of security. ...

• ### Augmented reality devices speed van repairs at Volkswagen U.K.

Augmented reality headsets for garage mechanics speed collaboration between repair shops and experts in the home office to solve ...

• ### Endpoint security tool fueled OpenText's Guidance Software acquisition

Endpoint security was the primary draw for OpenText's Guidance Software acquisition. But plans to improve e-discovery and data ...

## SearchHRSoftware

• ### Mindfulness technology helps companies bolster worker well-being

In discussing how to bring mindfulness programs into enterprises, vendors explain details on what works for employees, how ...

• ### Cost, doubt about tech hold back AI for HR investment

AI technology will improve the productivity of HR departments by eliminating many routine and transactional processes. But what ...

• ### Health and wellness benefits are exploding; are you keeping up?

A WorldatWork survey shows companies offered a wide variety of health and wellness benefits at an expanded rate last year. Is ...

Close