Help a programmer with very simple Linear Regression (ordinary least squares?)

Hi everyone,

I'm a programmer, so I don't have much understanding of statistics. But I am supposed to write a program for my company that predicts our inventory needs for the purchase orders, and my guess at the best model to use is the ordinary least squares regression line. I may need to use the logarithmic trend line instead, but you're the statistics experts, so use your best judgment.

Problem: I have no clue how to work out the formula... I'm going to have a hard enough time trying to convert the formula to an SQL statement, but that's another story.

Here's the data I need to figure out: I have to examine the percentage of increase number of a product sold for the past 4 months. I'd also like to average those increases with the corresponding month from the previous year. That should give me 3 numbers to graph as points where x=month and y=quantity sold.

I then need to use the linear regression to predict the quantity I need to order for the next 3 months. I can do ALL of this in Excel, but I need the formula to make this work in my SQL program. In fact, I've attached a screenshot of the graphical representation of the formula I'm trying to create.

Please, help a guy out!
My graphical representation

Sorry, forgot to attach the graph I did in Excel. This graph is actually a Logarithmic trend line.
Last edited:
all statistical software packages have implemented functions to perform linear regression. you don't need to write your own function.


TS Contributor
Just get your data from your SQL or whatever database into Excel and use LINEST() function [link] or the Data Analysis Toolpak [link ; link2]

If you consider more advanced modelling you can realte your database to R.