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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:59:58+00:00 2026-06-12T00:59:58+00:00

Im struggling to get this query to be able to order by stakes correctly

  • 0

Im struggling to get this query to be able to order by stakes correctly – it returns values that are neither desc or asc. It does work with wins if I use order by. Perhaps MySQL did not evaluate the sort order at the time it was building the aggregation?

I’ve stripped out some of the full names to make it less tedious to read.

SELECT a.b AS  t , COUNT(  c.aID ) AS  r , 
                   COUNT( 
                          CASE WHEN  c.Finish =1
                               THEN 1 
                               ELSE NULL 
                               END )               AS  wins , 
                 CONCAT( FORMAT( (
                                   COUNT( 
                                          CASE WHEN  c.Finish =1
                                               THEN 1 
                                               ELSE NULL 
                                               END ) / COUNT(  c.aID ) ) *100, 0 ) ,  '%'
                       ) AS  Percent , 
                FORMAT( SUM(  c.StakeWon ) , 0 ) AS  stakes 
 FROM c
 INNER JOIN a ON c.aID = a.aID
 INNER JOIN d ON c.dID = d.dID
 WHERE  d.w >= STR_TO_DATE(  '2012,07,01',  '%Y,%m,%d' ) 
 AND  d.w < STR_TO_DATE(  '2012,07,01',  '%Y,%m,%d' ) + INTERVAL 1 
 MONTH 
 GROUP BY  a.b 
 ORDER BY  stakes DESC`

It also doesnt work if I order by Percent. I didnt want to ask this question here but this is driving me crazy.

  • 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-12T00:59:59+00:00Added an answer on June 12, 2026 at 12:59 am

    It would help if you show some of the result, but you must sort the stakes before you format it. Then you really sort the numeric value rather than the formatted string.

    Here is an example SQL Fiddle how it would go wrong.

    The numeric values are not sorted descending because of the formatting

    100
    10,000,000
    -5,000,005
    

    So you would do something like

    ...
         FORMAT( SUM(  c.StakeWon ) , 0 ) AS  stakes ,
         SUM(  c.StakeWon )  AS  stakes_num 
    ...
         ORDER BY stakes_num desc
    

    Example how it would work: SQL Fiddle

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

Sidebar

Related Questions

Struggling to get this query working: $query_search = SELECT questionnaires_index.id, questionnaires_index.ea_num, questionnaires_index.address, questionnaires_index.status, questionnaires_index.json_stored
I've been struggling with this one SQL query requirement today that I was wondering
Im really struggling to get my head round this. Im using c#. I want
Ok so i've been struggling to get my head around this for a few
I am struggling to get my head around operator overloading. In this case the
I am struggling to get a subsonic select query to work, I am writing
I'm really struggling to get a query time down, its currently having to query
I'm struggling to get my head around XML namespace query when using XML nodes
I have been struggling to get this right! Can anyone help me to convert
I have been struggling to get this working. I wish to have an EF

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.