gpt4 book ai didi

sql - 显示多条记录的第二个结果 - MSSQL2005

转载 作者:行者123 更新时间:2023-12-03 09:27:00 24 4
gpt4 key购买 nike

我有一个查询,显示一位客户的第二个结果。我现在需要做的是显示特定列表中每个客户的第二个结果(例如 20 个不同的客户)。

我该怎么做?通过 SSMS 2005 的 MS SQL2000

1 位客户的当前查询是

SELECT TOP 1 link_to_client, call_ref
FROM
(
SELECT TOP 2 link_to_client, call_ref
FROM calls WITH (NOLOCK)
WHERE link_to_client IN ('G/1931')
AND call_type = 'PM'
ORDER BY call_ref DESC
) x
ORDER BY call_ref

谢谢

最佳答案

您需要使用row_number()函数,尝试这样的事情:

select
link_to_client, call_ref
from
(
select
link_to_client, call_ref,
row_number() over (partition by link_to_client order by call_ref desc) n
from
calls with (nolock)
where
link_to_client in ('G/1931')
and call_type = 'PM'
) x
where
n = 2 -- second result for every client

关于sql - 显示多条记录的第二个结果 - MSSQL2005,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18506976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com