HELP !!!我很难过,并尝试了几个选项无济于事...... 我需要为每个Pub_id返回一行,并且返回的行应该是具有更高Count的行,并且当有多个具有最高计数的行时,我需要具有更高price_id的行。
我已用这些数据填充了表格......
pub_id, price_id, count
7, 59431, 5
22, 39964, 4
39, 112831, 3
39, 120715, 2
47, 95359, 2
74, 142825, 5
74, 106688, 5
74, 37514, 1
这就是我需要返回的......
pub_id, price_id, count
7, 59431, 5
22, 39964, 4
39, 112831, 3
47, 95359, 2
74, 142825, 5
答案 0 :(得分:5)
;WITH T
AS (SELECT *,
ROW_NUMBER() OVER (PARTITION BY pub_id
ORDER BY [count] DESC, price_id DESC) AS rn
FROM your_table)
SELECT pub_id,
[count],
price_id
FROM T
WHERE rn=1
答案 1 :(得分:0)
你想要这样的东西
select pub_id,
Count,
Price_Id
from (select Pub_id,
max(count),
Price_Id
from table_name
group by Pub_id) der_tab
group by Pub_id,
Count
having Price_id = max(price_Id)