Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7399865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:07:00+00:00 2026-05-29T04:07:00+00:00

I have some data in a sql database and I’d like to calculate the

  • 0

I have some data in a sql database and I’d like to calculate the slope. The data has this layout:

Date        |  Keyword  |  Score    
2012-01-10  |  ipad     |  0.12    
2012-01-11  |  ipad     |  0.17    
2012-01-12  |  ipad     |  0.24    
2012-01-10  |  taco     |  0.19    
2012-01-11  |  taco     |  0.34    
2012-01-12  |  taco     |  0.45    

I’d like the final output to look like this by creating a new table using SQL:

Date        |  Keyword  |  Score |  Slope    
2012-01-10  |  ipad     |  0.12  |  0.06    
2012-01-11  |  ipad     |  0.17  |  0.06    
2012-01-12  |  ipad     |  0.24  |  0.06    
2012-01-10  |  taco     |  0.19  |  0.13    
2012-01-11  |  taco     |  0.34  |  0.13    
2012-01-12  |  taco     |  0.45  |  0.13

To complicate things, not all Keywords have 3 dates worth of data, some have only 2 for instance.

The simpler the SQL the better since my database is proprietary and I’m not quite sure what formulas are available, although I know it can do OVER(PARTITION BY) if that helps. Thank you!

UPDATE: I define the slope as best fit y=mx+p aka in excel it would be =slope()

Here is another actual example that I usually manipulate in excel:

date        keyword         score       slope   
1/22/2012   water bottle    0.010885442 0.000334784  
1/23/2012   water bottle    0.011203949 0.000334784  
1/24/2012   water bottle    0.008460835 0.000334784  
1/25/2012   water bottle    0.010363991 0.000334784  
1/26/2012   water bottle    0.011800716 0.000334784  
1/27/2012   water bottle    0.012948411 0.000334784  
1/28/2012   water bottle    0.012732459 0.000334784  
1/29/2012   water bottle    0.011682568 0.000334784  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:07:00+00:00Added an answer on May 29, 2026 at 4:07 am

    The cleanest one I could make:

    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;
    

    It uses Simple Linear Regression to calculate the slope.

    Result:

    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
    

    Every database system seems to have a different approach to converting dates to numbers:

    • MySQL: TO_SECONDS(date) or TO_DAYS(date)
    • Oracle: TO_NUMBER(TO_CHAR(date, 'J')) or date - TO_DATE('1','yyyy')
    • MS SQL Server: CAST(date AS float) (or equivalent CONVERT)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have recently moved some data from an SQL Database instance to another one
If have a piece of code that gets some data from a sql database
I have some data with a timestamp in SQL Server, I would like to
I have some VB.net code that inserts data into my SQL database using a
I have a Microsoft SQL database, where i am trying to insert some data.
I have an some code to export data form an SQL database to an
I have to move some data from sql server to access database. In target
I have to migrate some legacy data from stand-alone sql server database to sharepoint
I have to insert some data periodically in my SQL Server database. But the
I have a form that is trying to insert some data into an SQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.