1

我努力让这个查询能够stakes正确排序 - 它返回既不是 desc 也不是 asc 的值。wins如果我使用 order by,它确实有效。也许 MySQL 在构建聚合时没有评估排序顺序?

我已经去掉了一些全名,以减少阅读的乏味。

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`

如果我订购它也不起作用Percent。我不想在这里问这个问题,但这让我发疯。

4

2 回答 2

2

如果您显示一些结果会有所帮助,但您必须在格式化之前对赌注进行排序。然后你真的对数值而不是格式化的字符串进行排序。

这是一个示例SQL Fiddle它会如何出错。

由于格式的原因,数值未按降序排序

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

所以你会做类似的事情

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

示例如何工作:SQL Fiddle

于 2012-09-29T10:27:59.783 回答
0

尝试使用整个表达式

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 )

在 order by 子句而不是别名中。我的观察是,别名不是这样工作的

大概是这样的

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  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 ) DESC`
于 2012-09-29T10:22:58.053 回答