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

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

## SearchDataManagement

• ### Oracle brings GoldenGate data integration service to cloud

Oracle is making its GoldenGate real time data technology available on its second-generation Oracle Cloud Infrastructure platform...

• ### Era Software raises \$15.25M for enterprise data management

The startup that began as the EraDB time series database is advancing its efforts with new funding and a cloud service for its ...

• ### Dgraph GraphQL database users detail graph use cases

Graph DB vendor Dgraph Labs is expanding its AWS cloud footprint with new regions and adding change data capture capabilities in ...

• ### TigerGraph unveils support for GCP, adds new connectors

Graph database vendor TigerGraph unveiled support for Google Cloud and new connectors to Snowflake and Tableau on April 21 during...

• ### Startup Veezoo emerges from stealth with NLQ-based platform

Aiming to be 'Siri for enterprises,' an analytics startup emerged from stealth with a platform that enables users to interact ...

• ### 15 data science tools to consider using in 2021

Numerous tools are available for data science applications. Read about 15, including their features, capabilities and uses, to ...

## SearchSAP

• ### S/4HANA Cloud SaaS ERP: Buying team overview

SAP's multi-tenant SaaS ERP, S/4HANA Cloud, is a viable choice for companies that need ease in their infrastructure management. ...

• ### SAP forms financial services partnership with Dediq

SAP and financial industry investment firm Dediq are forming a new business unit to develop applications that help banks and ...

• ### Unpatched applications threaten SAP security

Cyberattacks are a significant threat to unpatched, unprotected SAP applications, according to a new threat intelligence report ...

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

• ### Incorporate diversity and inclusion in technology design

DEI in technology is about more than creating a diverse workplace. We talked to a few DEI professionals about how teams build ...

• ### Microsoft previews OpenJDK distro to the delight of devs

In a move meant to attract more Java developers to its Azure cloud and further support the Java community, Microsoft launched a ...

• ### Supreme Court ruling on Java APIs eases developer worries

Now that the Supreme Court has ruled for Google over Oracle in their high-stakes copyright battle over Java APIs, developers can ...

## SearchDataCenter

• ### Nvidia SDK simulates quantum computing circuits on GPU systems

Nvidia edged its way into the quantum computing market with an SDK that simulates quantum circuits by adding horsepower to ...

• ### Programmable processor technology for next-gen data centers

The right processing technology can benefit your data center. Learn about advancements in CPU technologies, recent vendor ...

• ### Data processing units accelerate infrastructure performance

DPUs often run on networking packets to move information in the data center, instead of supporting processing workflows. Get an ...

## SearchContentManagement

• ### Hyland gets digital asset management tech with Nuxeo buy

By acquiring a smaller competitor's digital asset management platform, Hyland looks to build on its 2020 purchase of Alfresco, ...

• ### OpenText releases Cloud Editions content services updates

OpenText CE 21.2 includes federated document compliance that extends to Microsoft Office 365, along with a revamped content ...

As the pandemic disrupts paper workflows, Adobe courts small business users with simple webforms, digital signatures and payments...

## SearchHRSoftware

• ### Shift to HR shared services could save Connecticut millions

By consolidating 17 separate HR operations into one shared service, Connecticut expects to save significantly on costs, most of ...

• ### 10 steps to support your HCM system post go-live

To ensure a new system's success, HR leaders need to develop a plan for how they will support their new HCM system post go-live. ...

• ### Face mask detection a newcomer to employee surveillance

Face mask detection has emerged as another form of employee surveillance technology, and its adoption may be helped indirectly by...

Close