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

# Unpredictable consecutive rows

## I want to compare consecutive rows in a table, e.g., 1-2, 2-3, etc. But how do I do this, as none of the columns are predictable?

I want to compare consecutive rows in a table, e.g., 1-2, 2-3, etc. Now I was thinking of doing a self join for this. But the problem is, how do I do this, as none of the columns are predictable. For example, if I have the following data:

```name quantity cost
A      2      20
H      7      24
L      1      12
...```

and I want to find out the difference between the cost of A and H, H and L, and so on.

Interesting question. We need to join each row to the row which has the lowest name of all the names that are greater than the name in the row being joined. Simple, yes? I mean, once you say it like that, it's easy to understand, right?

Well, once you say it like that, it's easy to write the SQL for, too.

```select t1.name
, t1.cost as t1_cost
, t2.cost as t2_cost
, t2.cost - t1.cost as diff
from yourtable as t1
left outer
join yourtable as t2
on t2.name
= ( select min(name)
from yourtable
where name > t1.name )

name t1_cost t2_cost diff
A     20      24      4
H     24      12    -12
L     12      --     -- ```

It's an unusual ON condition, because you don't see one like this every day. It may be rare but it is perfectly valid.

Rephrasing a question may seem difficult, but, like everything else that's worth doing in this life, it gets easier the more often you do it. To be fair, though, perhaps one must know in advance how to rephrase the question the way I did, or even that this type of join was possible.

This is a nice example to remember. Thank you for the interesting question.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Oracle Data Science efforts advance with new services

Oracle adds news new services to its cloud infrastructure platform in a bid to provide data scientists, as well as data analysts,...

• ### Data governance metrics: Data quality, data literacy and more

Statistics on data quality are the most common metrics used to track data governance programs, but other types of data-related ...

• ### Snowflake raises \$479 million to boost cloud data warehouse

Snowflake is looking to grow its data warehouse platform with new funding and a partnership with Salesforce that could ...

• ### Yellowbrick Data, MicroStrategy partnership aims to enhance analytics

The new partnership will enable flexible scalability, support for mixed workloads and multi-cloud support. The goal is to provide...

• ### Ethical data collection in the spotlight over new regulations

New regulations have put data privacy top of mind for many consumers. Here's a look at how businesses can incorporate ethical ...

• ### Regulatory approval allows Google acquisition of Looker to close

Google's purchase of analytics vendor Looker was approved by a United Kingdom regulatory agency, more than eight months after the...

## SearchSAP

Business Suite 7 will receive mainstream maintenance until 2027, a decision industry observers say reflects the difficulty of an ...

• ### SAP cloud revenue up, S/4HANA adoption still slow

Although SAP reported impressive revenue and cloud growth for 2019, it remains challenged to get the majority of its installed ...

• ### SAP ECC to S/4HANA migration: 4 essential issues

The ERP landscape is transforming and SAP customers in particular are dealing with decision overload related to ECC to S/4HANA ...

## SearchSQLServer

• ### SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• ### SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• ### Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

## TheServerSide.com

• ### Why the 8 Java primitive data types are not objects

What's the difference between Java primitive types and objects? To start, don't classify primitive types as objects. Let's ...

• ### A brief history of Java: How it forever changed programming

The Java programming language is an important tool in an application developer's quiver. But Java has also undergone many changes...

• ### How to properly perform Java String comparisons

Don't let incorrect Java String object comparisons bog down your code. Follow this tutorial to understand the differences between...

## SearchDataCenter

• ### Initiative aims to improve data center incident reporting

The Data Center Incident Reporting Network hopes to pull back the smoke screen on software and hardware issues to improve ...

• ### A guide to GPU implementation and activation

Data center GPU usage goes beyond hardware. Admins must work with vendors and developers to have the right software architecture ...

• ### Key components for negotiating an HPC colocation contract

When it comes to HPC colocation, there are specialized areas to cover in a service-level agreement. Review provider protocols for...

## SearchContentManagement

• ### Akeneo rolls out newest version of PIM software

Akeneo PIM version 4.0 features digital asset manager, API-integrated connection modules and AI attribute mapping to better ...

• ### Get to know 4 workflow automation tools

Workflow automation can benefit businesses by making manual processes digital, giving employees more time to work on other tasks....

Organizations that sell content might benefit from a paywall. Here are some questions to ask when deciding if a paywall strategy ...

## SearchHRSoftware

• ### 8 HR technology trends CHROs should investigate in 2020

Many emerging HR technologies are solidly in the realm of 'must-haves,' while others remain hype. Here's some advice to help you ...

• ### How to attract top talent in a competitive hiring market

Finding the best candidates can seem impossible in today's competitive hiring landscape. Use these methods to help you attract ...

• ### Federal workforce too reliant on degrees, says White House

In its budget proposal, the Trump administration complains that hiring rules hurt its ability to hire those just out of college ...

Close