按SQL的降序组织两列

时间:2018-03-17 01:43:55

标签: mysql sql

我发现代码应该有效,我正在尝试按降序组织数据,但是,当我输入此代码时,我只按降序排列第一个,而不是两者。我想要两个降序,我该怎么做?这是我的代码。这样做的总体目标是确保NumberOfPlays和FourthDownConversions按形式降序排列。有人可以在这帮忙吗?

SELECT Form, Down/Dist as AverageDistance, Play/Down AS NumberOfPlays, Gain/Down AS FourthDownConversionRate
FROM footballOffStats
WHERE Form IS NOT NULL AND Down = 4 AND Form != "" AND Gain IS NOT NULL
ORDER BY FourthDownConversionRate DESC, NumberOfPlays DESC;

这些是产生的结果,我怎样才能按降序获得NumberOfPlays和FourthDownConversion?

Form    AverageDistance NumberOfPlays   FourthDownConversionRate
EARLY LT    4   109.25  1.25
DIAMOND 2   34.25   0
DBL FLEX    0.4444  24.5    4.25
SHIFT TO EMPTY  0.4 24  5
BLUE LIZ    0.8 23.75   1
BLUE RIP    0.5 23.75   1
BLUE RIP    0.5 23.5    2.25
DBL FLEX SNUG   1   23.25   0
RIP HIP 1.3333  23  5.75
Ace 0.5714  22.5    8.75
ACE RT  0.5714  22.5    3.75
ACE 0.4 22.5    1.75
Early Rip   0.6667  22.5    1.75
QUAD FLEX   0.8 22.5    1.25
ACE 0.8 22.5    1.25
BLUE RIP    0.3077  22.5    0
BLUE RIP    0.4 22.5    0
ACE 0.4 22.5    0
EARLY RIP   1.3333  22.5    0
SEATTLE 1.3333  22.5    0
BLUE RIP    0.5 22.5    0
BLUE LIZ    0.5714  22.5    -1.25
ACE 2   22  3.25
BLUE RIP    1   22  0
Blue Rex    4   22  0
BLUE RIP    0.4 21  13
BLUE RIP    0.16    21  0
EARLY RT    0.4 20.25   9.25
Rip Weak    0.25    20.25   9
Ace 0.4 20  10.75
JUMBO   2   19.5    0.5
HURRY   4   19.5    0.5
TOWERS  4   19.25   0.5
JUMBO   2   19  0.25
BIG GREEN   4   19  0
Blue Liz    0.5714  18.75   10.25
Liz Hip 0.8 18.75   4
DIAMOND 4   18.75   0
Blue LIz    0.2857  18.75   0
DIAMOND 1   18.5    3
WHACKY  4   18.25   12.25
WHACKY  4   18.25   0.75
BIG GREEN   4   18  0.5
BLUE    0.6667  17  -1
BIG GREEN   4   15  0
EMPTY LT    0.8 14  2
ACE 1.3333  14  1
TRIPS LT FLEX   1.3333  14  0
DBL FLEX    1.3333  13.75   1.5
DBL FLEX    0.4 13.25   2.75
DBL FLEX    1.3333  13  1.25
ACE 4   10.5    -0.5
BLUE LT 1   9.25    1.25
BLUE LIZ    1.3333  9.25    0.75
LEX 4   9.25    0
BLUE SEATTLE    2   9   1.5
REX 1.3333  9   0.75
EARLY RT    4   8.75    1.25
REX 4   8.75    0.5
BLUE RIP    0.2353  8.5 2.75
BLUE REX    4   8.5 0.75
BLUE REX    4   8.5 0
JUMBO WING  2   2   1.5
Ace Lt  2   0.5 0.75
JUMBO   4   0.5 0.5
LIZ WK  4   0   17.5
Blue Liz    1.3333  0   9.25
WHACKY  4   0   4.5
HURRY BG    4   0   4.25
ACE LT  4   0   2.25
TRIPS FLEX  0.5714  0   2
BROWN LT    0.8 0   1.5
ORANGE REX  4   0   1.5
BLUE RIP STACK  1.3333  0   1.5
BLUE LIZ    4   0   1.25
BUNCH LT    1   0   1.25
ACE 0.6667  0   1.25
ACE 2   0   1.25
Weak Lex    4   0   1
ACE 4   0   1
DIAMOND 4   0   0.5
Hurry   4   0   0.25
BLUE LT Flop    0.6667  0   0
BLUE BOSTON 0.4 0   0

3 个答案:

答案 0 :(得分:0)

如果在column1中有2个或更多行具有相同的值,则只会在column2上进行排序。它就像一个备用排序。

请尝试进一步解释您的问题,我们会尽力而为。

SELECT ROWNUM as t2.rn, t2.column1 FROM TABLE1 t2 ORDER BY t2.column1 DESC
FULL OUTER JOIN
SELECT ROWNUM as t1.rn, t1.column2 FROM TABLE1 t1 ORDER BY t1.column2 DESC
ON t1.rn = t2.rn

上述内容可能会给你你想要的结果,但你会失去你的数据完整性,所以你可能想重新思考。听起来你实际上最好做两个单独的查询,实际上并没有真正需要加入它们。

答案 1 :(得分:0)

你不能同时对两个列数据进行排序,记住你是排序记录而不是列,数据库将按第一列排序记录,然后当第一列具有相同的时候由第二列重新组织数据值

所以,例如:

---------------
| col1 | col2 |
---------------
| 2    | 2    |
| 2    | 3    |
| 3    | 1    |
| 1    | 3    |

按col1排序,然后按col2排序,两者都将输出

---------------
| col1 | col2 |
---------------
| 3    | 1    |
| 2    | 3    |
| 2    | 2    |
| 1    | 3    |

永远不会输出

---------------
| col1 | col2 |
---------------
| 3    | 3    |
| 2    | 3    |
| 2    | 2    |
| 1    | 1    |

答案 2 :(得分:0)

如果表中有一个具有相同名称的列,则会解释您的查询行为。

因此,您可以尝试重复order by

中的表达式
SELECT Form, Down/Dist as AverageDistance, Play/Down AS NumberOfPlays, Gain/Down AS FourthDownConversionRate
FROM footballOffStats
WHERE Form IS NOT NULL AND Down = 4 AND Form <> '' AND Gain IS NOT NULL
ORDER BY Gain/Down DESC, Play/Down DESC;