मैं सबसे साफ-सुथरा बना सकता था:
SELECT
Scores.Date, Scores.Keyword, Scores.Score,
(N * Sum_XY - Sum_X * Sum_Y)/(N * Sum_X2 - Sum_X * Sum_X) AS Slope
FROM Scores
INNER JOIN (
SELECT
Keyword,
COUNT(*) AS N,
SUM(CAST(Date as float)) AS Sum_X,
SUM(CAST(Date as float) * CAST(Date as float)) AS Sum_X2,
SUM(Score) AS Sum_Y,
SUM(CAST(Date as float) * Score) AS Sum_XY
FROM Scores
GROUP BY Keyword
) G ON G.Keyword = Scores.Keyword;
यह सिंपल लीनियर रिग्रेशन का उपयोग करता है ढलान की गणना करने के लिए।
परिणाम:
Date Keyword Score Slope
2012-01-22 water bottle 0,010885442 0,000334784345222076
2012-01-23 water bottle 0,011203949 0,000334784345222076
2012-01-24 water bottle 0,008460835 0,000334784345222076
2012-01-25 water bottle 0,010363991 0,000334784345222076
2012-01-26 water bottle 0,011800716 0,000334784345222076
2012-01-27 water bottle 0,012948411 0,000334784345222076
2012-01-28 water bottle 0,012732459 0,000334784345222076
2012-01-29 water bottle 0,011682568 0,000334784345222076
ऐसा लगता है कि हर डेटाबेस सिस्टम में तारीखों को संख्याओं में बदलने का एक अलग तरीका होता है:
- MySQL:
TO_SECONDS(date)
याTO_DAYS(date)
- ओरेकल:
TO_NUMBER(TO_CHAR(date, 'J'))
याdate - TO_DATE('1','yyyy')
- MS SQL सर्वर:
CAST(date AS float)
(या समकक्षCONVERT
)