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

# Runs in groups

## Rudy Limeback addresses the classic "runs" problem in SQL.

We have a table as below:

```create table goods (
barcode  number(15,0) not null primary key,
category number(15,0) not null,
num      number(15,0) not null,
mark     varchar2(10) not null )```

The data of the table as below:

```barcode  category   num  mark
9679839  100003894  212   E
9687165  100003894  213   E
9680883  100003894  214   I
9710863  100003894  515   E
9681246  100003894  516   E
9682695  100003894  517   E
9681239  100003894  518   E
9685409  100003894  519   E
9679843  100003894  520   C
9679844  100003894  521   C
9714882  100003894  522   E
9679845  100003894  523   I
9681211  100003894  524   E
9681216  100003894  525   E```

The expected query result is as follows:

```category  mark  start end
100003894  E    212   213
100003894  I    214
100003894  E    515   519
100003894  C    520   521
100003894  E    522
100003894  I    523
100003894  E    524   525```

Thanks.

This is the classic "runs" problem.

```select category
, mark
, start
, case when start = end
then null
else end end as end
from (
select dt.category
, dt.mark
, min(dt.num) as start
, max(dt.num) as end
from (
select s1.category
, s1.mark
, s1.num
, s1.num -
( select count(*)
from goods as s2
where s2.category = s1.category
and s2.mark     = s1.mark
and s2.num     <= s1.num )
as diff_rank
from goods as s1
) as dt
group
by dt.category
, dt.mark
, dt.diff_rank
) as dt2
order
by category
, start```

For the solution, I am indebted to Joe Celko's SQL for Smarties, 3rd edition, page 556. Please do not ask me to explain it. Instead, test it yourself, by building up your query starting at the DT subquery. Analyze the results carefully. The outermost query is merely for cosmetic purposes.

Incidentally, this is the first time I can recall ever writing ELSE END END AS END in SQL. This may not work in every database system, so use identifier delimiters as necessary.

#### 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 ...

• ### MicroStrategy analytics suite fosters insurance co-op's growth

Using MicroStrategy's BI platform, a Canada-based insurance co-operative has been able to spread analytics to end users ...

• ### 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 ...

## SearchSAP

• ### Take care of data before SAP S/4HANA migration

In this Q&A, Rajesh Rengarethinam of ERP security vendor Appsian discusses why reviewing data security and business processes are...

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 ...

## 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