如何通过在 SQL 服务器中以两种不同的方式做空来进行排名?

How to make Ranking by shorting in Two different way in SQL Server?

我要做Ranking,如果是负数,就升序排列,如果是正数,就降序排列

select
    Itemcode,
    isnull(sum(ss.DiscQty * ss.Cost),0) DescCost,
    RANK()OVER(Partition by Itemcode order by 
        case when isnull(sum(ss.DiscQty * ss.Cost),0) < 0 THEN isnull(sum(ss.DiscrepancyQty * ss.Cost),0) END ASC,
        case when isnull(sum(ss.DiscQty * ss.Cost),0) > 0 THEN isnull(sum(ss.DiscQty * ss.Cost),0) END DESC
    ) RANKS
from
    ss
Group by
    ItemNo

期待结果

ItemCode    DiscQty    Rank
===========================
111         -5000       1
121         -4500       2
222          10000      3
223          3000       4

但我得到的所有排名都是 1,

I just want to sort the DiscQty in Asc When DiscQty < 0

and DiscQty in Desc Order When `DiscQty > 0

你想要这样的多个键:

RANK() OVER (ORDER BY (CASE WHEN SUM(ss.DiscQty * ss.Cost) < 0 THEN SUM(ss.DiscQty * ss.Cost) ELSE 0 END) ASC,
                      SUM(ss.DiscQty * ss.Cost) DESC
) RANKS

您不需要 PARTITION BY