Q

# Using SQL to forecast growth

I have a simple table, it has a date column and a column with values that reflect the total disk space occupied...

by a database. Given that I have values for all preceding dates, how can I predict future values using SQL or PL/SQL , i.e. trend prediction.

Assume a simple table:

```Col1     Col2
JAN      1000
FEB      1300
MAR      1800
APR      2100
```
...and so on. How would I predict the value for any month in the future?

This is probably one of those problems more easily solved with a spreadsheet application such as Microsoft Excel. However, the SQL challenge has been put forth, and it is my obligation to accept. I came up with two different solutions: one for linear growth, and one for exponential growth. However, their implementations are very similar. Which solution you use depends on the type of thing you are measuring. For example, linear growth would be evident in your car's odometer, where population growth tends to be exponential.

The solutions involves calculating an addend for linear growth and a multiplier for exponential growth. They also use a helper table to assist in extending out the forecast. Let's start by creating our helper table. Its utility will become evident shortly.

```create table Cardinals
( digit numeric(1) primary key
check (digit >= 0)
);

insert into Cardinals values (0);
insert into Cardinals values (1);
insert into Cardinals values (2);
insert into Cardinals values (3);
insert into Cardinals values (4);
insert into Cardinals values (5);
insert into Cardinals values (6);
insert into Cardinals values (7);
insert into Cardinals values (8);
insert into Cardinals values (9);
```
This table simply holds the numbers 0 through 9. By cross joining the Cardinals table to itself and using each instance of the table for a different order of magnitude, we can produce a view that returns numbers 0 through 99. We will use this view as the basis for our forecast consisting of one hundred time periods.
```create view TwoDigitCardinals
as
select ( Tens.Digit * 10 ) + Ones.Digit Cardinal
from Cardinals Ones
cross join
Cardinals Tens;
```
(Please note, if your database doesn't support the SQL-92 CROSS JOIN syntax, you should be able to accomplish the same by simply separating the two instances of the table by a comma with no WHERE clause.) Incidentally, by cross joining even more instances of the Cardinals table, we can create queries that return even larger series of numbers. Now, let's create a table to store our history. It's pretty generic, with time periods and magnitude represented by simple integers. Without too much difficulty, we could modify this example to use dates to solve the original request. For now, I'll try to make my example as simple as possible for the sakes of both understanding and brevity. Here's the History table:
```create table History
( TimePeriod smallint primary key,
Magnitude integer
);

insert into History values (1, 1000);
insert into History values (2, 1300);
insert into History values (3, 1800);
insert into History values (4, 2100);
```
For linear growth, I decided to take the average of the increases from one period to the next as the basis for future growth. The query could be easily modified to consider only the last five or ten time periods. Here it is:
```select avg(H.Magnitude - PrevH.Magnitude) Addend
from History H
inner join
History PrevH
on H.TimePeriod-1 = PrevH.TimePeriod;

------
366
```
Mathematically, my formula for linear growth looks like f(x) = a*x + b, where x is the time period extending into the future, and f(x) is the projected magnitude.

In order to make the final SQL a bit easier to understand, I'll illustrate it first, with pseudo-SQL using some simplified tokens encased in greater-than and less-than symbols:

```select Cardinal + 1 + <Last Recorded TimePeriod>,
<Magnitude of the Last Recorded TimePeriod>
+
( <Addend> * ( Cardinal + 1 ) )
from TwoDigitCardinals
order by Cardinal;```

This was last published in March 2002

## Content

Find more PRO+ content and other member only offers, here.

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

• ### Graph data model cements tight relationships between data elements

Graph databases can help define and discover relationships between entities -- and offer increased flexibility and better ...

• ### Microsoft SQL Server 2016 relational DBMS overview

Microsoft SQL Server 2016 for Windows comes in four editions, with updates that include a new stretch database feature, Polybase,...

• ### Amazon ushers in pay-by-query pricing with Athena data engine

Amazon's Athena data engine brings interactive SQL queries to S3. It's based on an open source framework called Presto that ...

• ### Creative projects leave people guessing about future impact of AI

A push is underway to write creative AI algorithms that can engage in music, film and design projects. So far, they have ...

• ### Data visualization plays an important role in a big data environment

Enterprises are increasingly putting data visualization tools, like Tableau, at the top of their big data platforms to start ...

## SearchSAP

• ### Does SAP ONE Support Launchpad make SAP support any easier to use?

The new Fiori user experience makes it easier to access applications and support services, but product-specific support still ...

• ### At SME Summit, SAP Anywhere growing pains revealed

Attendees at an SAP SME Summit lauded the e-commerce front end, but some said back-end integration and payment-processing ...

• ### Planning a HANA big data strategy with SAP HANA Vora

SAP has worked hard to position HANA as a big data platform. To formulate a viable big data strategy, you need to know the tools,...

## SearchSQLServer

• ### How to get the most out of virtual SQL Server with Microsoft Hyper-V

SQL Server is a CPU-intensive technology, which can make it tricky to run in a virtualized environment. Keep your SQL Server ...

• ### Microsoft previews SQL Server on Linux, opens features across editions

Microsoft looks to broaden the horizons of SQL Server, as it moves some Enterprise features to Standard Edition and issues the ...

• ### Cask framework aims to speed Azure HDInsight data pipeline builds

A link between Cask Data's CDAP application and integration environment and Azure HDInsight, Microsoft's Hadoop cloud service, is...

## TheServerSide

• ### How to turn your DevOps failures into ALM successes

Doing the right thing doesn't always mean you're doing things right. But don't fret, because short-term DevOps failures can mean ...

• ### From chatbots to IBM's Watson: How software deals with conversational language

The next big thing in software development is conquering the conversational language development hurdle. Here's how the big ...

• ### Getting through the three toughest stages of a DevOps transition

Looking to gain increased productivity and software quality by embracing DevOps? Here are the DevOps transitions your company ...

## SearchDataCenter

• ### Challenges of IoT include big data, data analysis for enterprise

Implementing big data and IoT is difficult for enterprise IT teams due to major challenges on the network. Here's how IT can ...

• ### Compare benefits of a blade server architecture vs. hyper-convergence

To determine if a series of blade servers is the right call for more condensed compute in your data center, evaluate its benefits...

• ### A hyper-converged data center offers SDDC opportunity, IT scalability

Hyper-convergence can increase scalability and pave the way toward software-defined infrastructure. Use these five tips to learn ...

## SearchContentManagement

• ### Gemological Institute of America gives content automation high grades

The Gemological Institute of America used Quark to automate content, cutting the process by a third.

• ### Five steps for designing an effective BI dashboard

The BI dashboard can be a very useful tool for data engagement if the dashboard is designed and used effectively. Here are five ...

• ### Examining the top offerings in the WCM platform marketplace

Once you've decided that a WCM platform is right for your business, it's time to decide which to choose. Expert Geoffrey Bock ...

## SearchFinancialApplications

• ### No one-size-fits-all strategy for cloud ERP software migration

Experts say a cloud ERP transition plan will vary according to a variety of factors, from company size to an organization's ...

• ### HR video apps spreading beyond training to recruiting, engagement

Users say cost savings, faster turnaround and better corporate branding are some of the benefits from using video at more stages ...

• ### Microsoft picks SAP for core HCM tools and global talent management

Microsoft chooses SAP SuccessFactors for core HR and talent management, and SAP selects Microsoft Azure as a preferred public ...

Close