可能最好我用一个例子来解释我所追求的内容:)
说我有下表
SELECT *
INTO #OrderRanking
FROM (
SELECT 'Jim', 1000
UNION ALL
SELECT 'Jim', 500
UNION ALL
SELECT 'Jim', 500
UNION ALL
SELECT 'Jim', 3000
UNION ALL
SELECT 'Bob', 1000
UNION ALL
SELECT 'Bob', 2000
UNION ALL
SELECT 'Bob', 500
UNION ALL
SELECT 'Bob', 500
UNION ALL
SELECT 'Fred', 500
) AS t(Customer, OrderTotal)
我想要的是SELECT
声明中的以下结果
Customer, OrderTotal, CustomerId
'Jim', 1000, 1
'Jim', 500, 1
'Jim', 500, 1
'Jim', 3000, 1
'Bob', 1000, 2
'Bob', 2000, 2
'Bob', 500, 2
'Bob', 500, 2
'Fred', 500, 3
我的实际查询有点复杂,其中源列超过2。
答案 0 :(得分:2)
我是个傻瓜,
SELECT *, DENSE_RANK() OVER(ORDER BY Customer DESC)
FROM #OrderRanking
我认为这应涵盖所有情况!
答案 1 :(得分:1)
select customer, ordertotal, dense_rank() over(order by total desc, customer asc)
from (select *, sum(ordertotal) over(partition by customer) total from #orderranking) t