Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1123
  • Last Modified:

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
0
mosheshitrit
Asked:
mosheshitrit
  • 5
  • 4
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
muzzy2003Commented:
Change of table name is irrelevant, it's the variable names that you need to change.
0
 
mosheshitritAuthor Commented:
Thanks muzzy2003
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now