我想只从表中选择第二行。从ClientUserName列。
SELECT
ClientUserName, DestHost, count(DestHost) counts
FROM
#ProxyLog_record
WHERE
ClientUserName = (Select top 1 ClientUserName from #ProxyLog_count_2)
GROUP BY
ClientUserName, DestHost
ORDER BY
counts DESC
(Select top 1 ClientUserName from #ProxyLog_count_2)
仅显示top 1
,但我需要从该表中获取第二个数据。我怎么能这样做?
答案 0 :(得分:0)
据推测,您使用的是SQL Server。 “前1”是从表中选择一个随机行,因为你没有order by子句。
如果要将第二行插入表中,则执行此操作的方法是在表中定义自动递增列。 create table语句应包括:
create table #ProxyLog_count_2 (
ProxyLog_Count_2_id int not null identity(1,1),
...
)
然后,您可以使用simple where子句插入第二行:
where ProxyLog_Count_2_id = 2
答案 1 :(得分:0)
最简单的方法是使用ROW_NUMBER()
方法,如下所示:
WITH c AS (
SELECT
ClientUserName, DestHost, count(DestHost) counts, ROW_NUMBER() OVER(ORDER BY count(DestHost)) AS rowNum
FROM
#ProxyLog_record
GROUP BY
ClientUserName, DestHost
)
SELECT ClientUserName, DestHost, counts
FROM c
WHERE rowNum = 2
(如果不需要,请不要使用count(DestHost) counts
,或使用其他CTE两次保存计数)
答案 2 :(得分:0)
根据你到目前为止所尝试的内容......
SELECT top 1
ClientUserName, DestHost, count(DestHost) counts
FROM
#ProxyLog_record
WHERE
ClientUserName <> (Select top 1 ClientUserName from #ProxyLog_count_2)
GROUP BY
ClientUserName, DestHost
ORDER BY
counts DESC
您可以使用ROW_NUMBER()...
您可以使用光标......
您可以在顶行添加一个标识符,以便您将其过滤掉(然后再选择前1名,然后您就可以了。)
否则,如果第二行有可识别的东西(它有最高的东西,或最低的,或最近的日期等),
答案 3 :(得分:0)
尝试:
SELECT
ClientUserName, DestHost, count(DestHost) counts
FROM
#ProxyLog_record
WHERE
ClientUserName = (
;with cte as
(
select ROW_NUMBER() over (order by ClientUserName) as rn, *
from #ProxyLog_count_2
)
select ClientUserName from cte where rn=2
)
GROUP BY
ClientUserName, DestHost
ORDER BY
counts DESC
答案 4 :(得分:0)
Wjy不只是选择前2并从结果集中取第二行?
答案 5 :(得分:0)
SELECT Top 1 a.ClientUserName, a.DestHost, a.counts
FROM
(
SELECT Top 2
ClientUserName, DestHost, count(DestHost) counts
FROM
#ProxyLog_record
WHERE
ClientUserName = (Select top 1 ClientUserName from #ProxyLog_count_2)
GROUP BY
ClientUserName, DestHost
ORDER BY
counts DESC
)
as a
ORDER BY a.Counts ASC