0

我只想从表中选择第二行。从 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,但我需要从该表中获取第二个数据。我怎样才能做到这一点?

4

6 回答 6

0

据推测,您正在使用 SQL Server。“前 1”是从表中选择一个随机行,因为您没有 order by 子句。

如果要将第二行插入到表中,方法是在表中定义一个自增列。创建表语句应包括:

create table #ProxyLog_count_2 (
    ProxyLog_Count_2_id int not null identity(1,1),
    ...
)

然后,您可以使用简单的 where 子句插入第二行:

where ProxyLog_Count_2_id = 2
于 2012-06-27T15:12:42.160 回答
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节省两次计数)

于 2012-06-27T15:14:03.717 回答
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 并获得它)。

否则,如果第二行有一些可识别的东西(它是否有最高的东西,或最低的,或最近的日期等),

于 2012-06-27T15:14:32.203 回答
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
于 2012-06-27T15:14:45.510 回答
0

Wjy 不只是选择前 2 名并从结果集中获取 aecomd 行吗?

于 2012-06-27T16:16:20.697 回答
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
于 2013-12-06T05:48:05.093 回答