Regression in SQL

Hi,

I need to create a daily regression on a table with 3 columns (ItemCode, Date, SaleQty).
I tried working with “LinRegIntercept” but couldn’t understand how to work with “LastPeriods”.
Is this the right way to work?
Can someone send me a full example?

10X
mosheshitritAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

muzzy2003Commented:
You are in Analysis Services, right? These are MDX not SQL expressions. Either way, please explain more about what you are trying to do.
0
mosheshitritAuthor Commented:
Hi muzzy2003,

I guess you are right.
Should I install or do anything before working with MDX?
Does it have a driver?

10X


0
muzzy2003Commented:
MDX is a querying language for use with an Analysis Services cube. If you aren't using one of these, you need to find another solution to your problem. You can code all the linear regression formulae using the in-built AVG, STDEV, STDEVP functions of SQL, but that's as much help as the language will give you I'm afraid.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mosheshitritAuthor Commented:
Does someone by any chance have a sample of a regression function written in SQL?


0
muzzy2003Commented:
If you have a table with a list of values x, y, then you can get the regression parameters a (intercept), b (gradient), and r (regression coefficient) like this:

DECLARE n float, Sx float, Sy float, Sxx float, Syy float, Sxy float, a float, b float, r float

SELECT  n = COUNT(*), Sx = SUM(x), Sy = SUM(y), Sxx = SUM(x * x), Syy = SUM(y * y), Sxy = SUM(x * y)
FROM    Table

SET b = (n * Sxy - Sx * Sy) / (n * Sxx - Sx * Sx)
SET a = (Sy - b * Sx) / n
SET r = (n * Sxy - Sx * Sy) / SQRT((n * Sxx - Sx * Sx) * (n * Syy - Sy * Sy))
0
mosheshitritAuthor Commented:
muzzy2003,

I created a table with two float columns (X, Y).

I get the following error:
'float' is not a recognized CURSOR option.

10X
0
muzzy2003Commented:
Sorry, my mistake:

DECLARE @n float, @Sx float, @Sy float, @Sxx float, @Syy float, @Sxy float, @a float, @b float, @r float

SELECT  @n = COUNT(*), @Sx = SUM(x), @Sy = SUM(y), @Sxx = SUM(x * x), @Syy = SUM(y * y), @Sxy = SUM(x * y)
FROM    #tmpdm

SET @b = (@n * @Sxy - @Sx * @Sy) / (@n * @Sxx - @Sx * @Sx)
SET @a = (@Sy - @b * @Sx) / @n
SET @r = (@n * @Sxy - @Sx * @Sy) / SQRT((@n * @Sxx - @Sx * @Sx) * (@n * @Syy - @Sy * @Sy))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
muzzy2003Commented:
Change of table name is irrelevant, it's the variable names that you need to change.
0
mosheshitritAuthor Commented:
Thanks muzzy2003
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.