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

  • SEARCH
  • Home
  • 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 8508021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:02:21+00:00 2026-06-11T03:02:21+00:00

(Please refer to SQLFiddle for a working example of this post) I have a

  • 0

(Please refer to SQLFiddle for a working example of this post)

I have a table with stock information, as follows:

sp100_id  _date         bullishness  agreement
----------------------------------------------
1         2011-03-16    1.01          0.33
1         2011-03-17    0.85          1.28
1         2011-03-18    0.89          1.25
1         2011-03-21    1.46          1.21
1         2011-03-22    0.39          2.53
2         2011-03-16    3.07          1.27
2         2011-03-17    2.09          0.80
2         2011-03-18    0.91          0.12
2         2011-03-21    1.50          0.00
2         2011-03-22    2.62          1.10
3         2011-03-16    0.73          1.13
3         2011-03-17    1.13          1.21
3         2011-03-18    1.12          0.45
3         2011-03-21    1.00          1.01
3         2011-03-22    1.00          0.53
4         2011-03-16    0.40          1.10
4         2011-03-17    2.40          0.03
4         2011-03-18    3.16          0.10
4         2011-03-21    0.86          0.50
4         2011-03-22    1.00          0.10

I need to order the companies (sp100_id) by their averge bullishness over time into a top-3:

SELECT 
  sp100_id, 
  AVG(bullishness) as bullishness, 
  AVG(agreement) AS agreement 
FROM stocks 
WHERE _date BETWEEN '2011-03-16' AND '2011-03-22' 
GROUP BY sp100_id LIMIT 3

This works fine, as the result is

SP100_ID    BULLISHNESS AGREEMENT
2           2.038           0.658
4           1.564           0.366
3           0.996           0.866

Now that I have the top-3, I need the top-3 to be re-ordered by AGREEMENT, ascending:

SP100_ID    BULLISHNESS AGREEMENT
4           1.564           0.366
2           2.038           0.658
3           0.996           0.866

Is this possible to do with one query? I tried the following but it didn’t work. It still only orders by bullishness

SELECT 
  sp100_id, 
  AVG(bullishness) as bullishness, 
  AVG(agreement) AS agreement 
FROM stocks 
WHERE _date BETWEEN '2011-03-16' AND '2011-03-22' 
GROUP BY sp100_id 
ORDER BY bullishness DESC, agreement ASC LIMIT 3

So to be clear: (1) I need to find the top-3 companies with highest average bullsihness (2) this top-3 then needs to be ordered from lowest to highest agreement. Preferably with one query. Do you know how?

  • 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-11T03:02:22+00:00Added an answer on June 11, 2026 at 3:02 am

    It’s called structured query language because you can build structures in which queries (aka virtual tables) are nested inside other queries.

    Take your first query, which is correct except it needs its own ORDER BY clause, and nest it in another, like so.

    SELECT * 
      FROM (
               SELECT sp100_id,  
                      AVG(bullishness) as bullishness, 
                      AVG(agreement) AS agreement 
                 FROM stocks 
                WHERE _date BETWEEN '2011-03-16' AND '2011-03-22' 
             GROUP BY sp100_id
             ORDER BY bullishness DESC
                LIMIT 3
      ) subquery
      ORDER BY agreement ASC
    

    Go fiddle: http://sqlfiddle.com/#!2/c9ff0/7/0

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

Sidebar

Related Questions

Please refer to this post. I have become able to configure my web.config file
Please refer to this fiddle . While scrolling down, the table cells appear in
Please refer to this page for reference: http://loadedgranola.valitics.com/_product_83484/blackberry_lime I have a jQuery script that
Please refer this fiddle , I have tried to create an animation where a
I have a string like this: Please refer to document ABC.123.1234.1234 and document CBA.321.4321
Please refer my this post- how to change TTNavigator (for a web url) bottom
Please refer to this background question. After constructing this COUNT, how would I then
Please refer my previous post here . I did changes accordingly but getting error.
How does the toggle button on mootools rte work, please refer to example on
Please refer to my previous question here: Link Here Will this project type be

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.