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 8322969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:33:54+00:00 2026-06-08T23:33:54+00:00

(This is quite a long post, but the problem is I think easy to

  • 0

(This is quite a long post, but the problem is I think easy to solve and I have a SQLFiddle ready) Please consider the following table:

----------------------------------------------------------------------
tweet_id sp100_id nyse_date   user_id class_id retweets quality follow
----------------------------------------------------------------------
1        1        2011-03-12  1       1        0        2.50    5.00
2        1        2011-03-13  1       2        2        2.50    5.00
3        1        2011-03-13  1       2        1        2.50    5.00
4        1        2011-03-13  2       2        0        0.75    1.00
5        1        2011-03-13  2       3        3        0.75    1.00
6        2        2011-03-12  2       2        3        0.75    1.00
7        2        2011-03-12  2       2        0        0.75    1.00
8        2        2011-03-12  1       3        5        2.50    5.00
9        2        2011-03-13  2       2        0        0.75    1.00
----------------------------------------------------------------------

The desired output from this table is a list per sp100_id per _date the amount of positive (class=2) and negative (class=3) tweets weighted per retweets, quality and follow:

--------------------------------------------------------------------------------
sp100_id  nyse_date  pos-rt pos-quality pos-follow neg-rt neg-quality neg-follow
--------------------------------------------------------------------------------
1         2011-03-11 0      0           0          0      0           0
1         2011-03-12 0      0           0          0      0           0
1         2011-03-13 3 (1)  5.75 (2)    11.00 (3)  3 (4)  0.75        1.00
2         2011-03-11 0      0           0          0      0           0
2         2011-03-12 3      1.50        10.00      5.00   2.50        2.50
2         2011-03-13 0      0.75        1.00       0      0           0
--------------------------------------------------------------------------------

On 2011-03-13, 3 positive tweets for sp100_id 1:

(1) 1 tweet retweeted 2 times, 1 tweets retweeted 1 time and 
    1 tweet retweeted 0 times = 1 x 2 + 1 x 1 + 1 x 0 = 3
(2) 2 tweets with quality 2.50 and 1 tweet with quality 0.75 =
    2 x 2.50 + 1 x 0.75 = 5.75
(3) 2 tweets with follow 5 and 1 tweet with follow 1 =
    2 x 5.00 + 1 x 1.00 = 11.00

On 2011-03-13, 1 negative tweets for sp100_id 1:

(4) 1 tweet retweeted 3 times = 1 x 3 = 3

etc...

I have a demo on SQLFiddle with the necessary other tables (I need to link it to a daterange table because I also want to include recordsets with all zero’s). I also have an output for my query, but I don’t understand why it is different from the desired output:

--------------------------------------------------------------------------------
sp100_id  nyse_date  pos-rt pos-quality pos-follow neg-rt neg-quality neg-follow
--------------------------------------------------------------------------------
1         2011-03-11 0      0           0          0      0           0
1         2011-03-12 3      2           2          5      3           5
1         2011-03-13 3      8           12         3      1           1
2         2011-03-11 0      0           0          0      0           0
2         2011-03-12 3      2           2          5      3           5
2         2011-03-13 3      8           12         3      1           1
--------------------------------------------------------------------------------

I don’t see where the problem lies. Do you? Your help would be greatly appreciated 🙂

  • 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-06-08T23:33:56+00:00Added an answer on June 8, 2026 at 11:33 pm

    The reason why it wasn’t returning expected values is because you need to also include sp100.sp100_id = tweets.sp100_id in the LEFT JOIN condition along with the date.

    By only joining on the date, it will join on any date value in the table, regardless of sp100_id. This is why your resulting sums were being thrown off because for each sp100_id, it was including the values of all other sp100_ids in the SUM()s.

    I also cleaned up your query a little bit (just in terms of aesthetics):

    SELECT     a.sp100_id,
               b._date AS nyse_date,
               SUM(IF(c.class=2, c.retweets, 0)) AS 'pos-rt',
               SUM(IF(c.class=2, c.quality,  0)) AS 'pos-quality',
               SUM(IF(c.class=2, c.follow,   0)) AS 'pos-follow',
               SUM(IF(c.class=3, c.retweets, 0)) AS 'neg-retweet',
               SUM(IF(c.class=3, c.quality,  0)) AS 'neg-quality',
               SUM(IF(c.class=3, c.follow,   0)) AS 'neg-follow'
    FROM       sp100 a
    CROSS JOIN daterange b
    LEFT JOIN  tweets c ON a.sp100_id = c.sp100_id 
                       AND b._date = c .nyse_date
    GROUP BY   a.sp100_id, 
               nyse_date
    

    SQLFiddle Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this quite popular problem, but have failed to find a solution that
Firstly this has turned out to be quite a long post so please bear
Long time watcher and this is my first post so please go easy on
This post might be a bit long, but if you think you can help,
I've spent quite a long time Googling this and read various articles but I
Preface: This has become a quite a long post. While I'm not new to
I thought this was going to be easy, but it's turning into quite a
I've been sitting on this idea for quite a long time and would like
This is quite the pickle... I have a regular HTML form with a couple
I have a quite long and (for me) complex question. I have voting data

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.