avg列并在sql中对它们进行排序

时间:2012-06-07 02:45:30

标签: sql sql-order-by average

我需要订购这个平均值desc,但不知何故我不能。请帮忙

my query is something like this:

解决方案可能非常简单:)

2 个答案:

答案 0 :(得分:1)

我假设您要在左列中订购具有最高值的列。我还假设您使用的是SQL Server。从图片中看起来确实如此。

The solution is probably very simple.

不知道这一点。你告诉我......

with C(A, B, C) as
(
  -- Your query goes here
)
select [1],[2],[3]
from (
       select Val, row_number() over(order by Val desc) as rn
       from C unpivot (Val for Col in(A, B, C)) U
     ) T
pivot (min(val) for rn in ([1],[2],[3])) P

Proof of concept

答案 1 :(得分:0)

SELECT X FROM
(
    SELECT aa X FROM table
    UNION ALL
    SELECT bb X FROM table
    UNION ALL
    SELECT cc X FROM table
) a
ORDER BY X DESC

这样的事情会这样吗?