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

# Maximum of 5-day running totals

## I have a table of dates and number of events per date. I need to find a 5-day range where the most number of events happened. I just couldn't figure out a way to sum up the values of events-per-date for every 5 rows. This means all possible 5 consecutive rows, but dates are not necessarily consecutive (days where no events occurred are not kept).

I have a table of dates and number of events per date. I need to find a 5-day range where the most number of events happened. I just couldn't figure out a way to sum up the values of events-per-date for every 5 rows. This means all possible 5 consecutive rows, but dates are not necessarily consecutive (days where no events occurred are not kept).

```eventdate  numevents
2006-06-16    3
2006-06-17    3
2006-06-18    3
2006-06-19    3
2006-06-20    4
2006-06-21    4
2006-06-22    4
2006-06-23    4
2006-06-24    3
2006-06-25    2
2006-06-26    2
2006-06-27    2
2006-06-30    2
2006-07-01    2
2006-07-04    1```

This is only a very small section of the table.

What an interesting problem. Thanks for sending it in.

The solution requires a self-join. What we need to do is join the table to itself, with a special condition: each row must be joined only to itself and the immediately preceeding four rows. In other words, we join each row to the top 5 rows that have an eventdate that is equal to or less than that row. Please make sure you understand this statement of the problem before looking at the following SQL:

```select t1.eventdate
, t1.numevents
, t2.eventdate
, t2.numevents
from events as t1
inner
join events as t2
on t2.eventdate <= t1.eventdate
and 5 >=
( select count(*)
from events
where eventdate
between t2.eventdate
and t1.eventdate )
order
by t1.eventdate
, t2.eventdate```

The above query produces the following results:

```t1             t2
2006-06-16  3  2006-06-16  3

2006-06-17  3  2006-06-16  3
2006-06-17  3  2006-06-17  3

2006-06-18  3  2006-06-16  3
2006-06-18  3  2006-06-17  3
2006-06-18  3  2006-06-18  3

2006-06-19  3  2006-06-16  3
2006-06-19  3  2006-06-17  3
2006-06-19  3  2006-06-18  3
2006-06-19  3  2006-06-19  3

2006-06-20  4  2006-06-16  3
2006-06-20  4  2006-06-17  3
2006-06-20  4  2006-06-18  3
2006-06-20  4  2006-06-19  3
2006-06-20  4  2006-06-20  4

2006-06-21  4  2006-06-17  3
2006-06-21  4  2006-06-18  3
2006-06-21  4  2006-06-19  3
2006-06-21  4  2006-06-20  4
2006-06-21  4  2006-06-21  4

and so on```

Notice that the first four t1 dates do not have five preceeding t2 dates. We'll come back to that in a moment.

Now that we know we're looking at the right data, we can do the next step, which is to add up the numevents values for the joined t2 rows. We do this by changing the query into a GROUP BY query:

```select t1.eventdate
, count(*)          as rows
, sum(t2.numevents) as total
from events as t1
inner
join events as t2
on t2.eventdate <= t1.eventdate
and 5 >=
( select count(*)
from events
where eventdate
between t2.eventdate
and t1.eventdate )
group
by t1.eventdate```

The above query produces the following results:

```eventdate  rows total
2006-06-16  1     3
2006-06-17  2     6
2006-06-18  3     9
2006-06-19  4    12
2006-06-20  5    16
2006-06-21  5    17
2006-06-22  5    18
2006-06-23  5    19
2006-06-24  5    19
2006-06-25  5    17
2006-06-26  5    15
2006-06-27  5    13
2006-06-30  5    11
2006-07-01  5    10
2006-07-04  5     9 ```

Notice that we can add HAVING COUNT(*)=5 to this query if we wish to eliminate the first four dates.

The final step is to take the maximum of the total column, and there are a number of ways to do it, which we don't really have room to discuss. The easiest is to add ORDER BY total DESC to the above query, and, by inspection, take the first result row. Caution: look for ties for the maximum, as there are in this case for the 23rd and 24th.

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

• ### Amazon Quantum Ledger Database brings immutable transactions

Based on technology built internally at Amazon, the tech giant's newest database provides a centralized approach for enabling a ...

• ### Stibo Systems advances multidomain MDM system

The new Stibo Systems 9.2 update expands the MDM platform's features with Sisense business intelligence integration and machine ...

• ### Cloudera Data Platform to debut, as big data fortunes waver

The interim CEO of Cloudera is cautiously optimistic about growth prospects as the big data vendor acquired Arcadia Data and ...

• ### Oracle BI platform on the comeback trail

Time had seemingly left Oracle's business intelligence tools behind -- until the vendor responded by consolidating its BI ...

• ### BI for mobile remains a challenge for vendors

While some BI vendors have developed effective mobile apps that provide concise insights, those that have attempted to recreate ...

• ### Magento BI update a benefit to vendor's e-commerce customers

Magento rolled out the Magento Business Intelligence Summer 2019 Release, updating its BI platform with enhanced scheduling ...

## SearchSAP

• ### Navy sails SAP ERP systems to AWS GovCloud

The Navy consolidated 26 various ERP systems onto SAP HANA on the AWS GovCloud, leading to data reduction, lower TCO, and setting...

• ### SAP HANA application collects concussion data for real-time analysis

Protecht, an SAP HANA based system, enables teams in contact sports to measure and analyze data about hits though an IoT sensor ...

• ### S/4HANA public cloud vs. private cloud: Compare the pros, cons

In addition to SAP's on-premises and hybrid models, the vendor offers S/4HANA in public and private clouds. Here's a look at the ...

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

• ### Eclipse completes enterprise Java move with Jakarta EE 8

The Eclipse Foundation has assumed control of the Java EE enterprise platform and specifications from Oracle and has launched an ...

• ### AI development tools make software development easier

Software tools with AI can help optimize various elements to make the development pipeline easier to handle for all those ...

• ### Close Agile open source tools vulnerabilities

The State of the Software Supply Chain report highlights some developer team best practices to help increase productivity with ...

## SearchDataCenter

• ### IBM z15 mainframe secures data across multi-cloud environments

IBM unveiled the latest in its line of mainframes capable of processing 1 trillion web transactions a day. The IBM z15 ...

• ### Data center management as a service launches DCIM to the cloud

DMaaS is an option for organizations that want to use cloud-based management. Though these offerings hold a lot of promise, there...

• ### Decrease legacy hardware security gaps

Dated infrastructure poses security threats to organizations -- especially with the amount of sensitive data used today. Address ...

## SearchContentManagement

• ### Cohesion is latest Acquia acquisition

While Acquia would not provide specifics of the deal, the decision behind the acquisition of Cohesion was the chance to bring ...

• ### 3 things to learn before considering blockchain integration

Paul Swider, CTO at RealActivity LLC, discusses how to use blockchain technology to increase the security and usefulness of ...

• ### Microsoft PowerApps pricing proposal puts users on edge

Microsoft's PowerApps pricing changes, expected to take effect in October, could make them too expensive for some users who ...

## SearchHRSoftware

• ### HiQ Labs vs LinkedIn case OKs robot monitoring of employees

A U.S. Court of Appeals ruled on the LinkedIn vs HiQ lawsuit, which could help shape how services gather information on social ...

• ### MyPayrollHR collapse stirs allegations, questions, anger

The problems created by the closing of MyPayrollHR may take a long time to resolve. The FBI has been contacted and lawsuits are ...

• ### Before deploying recruitment chatbots, define use cases

HR departments can benefit from using chatbots for basic tasks, but the technology will eventually be able to handle more complex...

Close