从sql中的列中选择前3条记录

时间:2010-09-28 05:21:42

标签: sql sql-server-2005

如何从sql

中的列中选择前三条记录

7 个答案:

答案 0 :(得分:2)

利用Top

select  top 3 columnname from table 

有关SQL TOP Clause

的更多信息

答案 1 :(得分:1)

Select Top 1 col_name 
From (Select Top 3 col_name from tablename order by col_name desc)

答案 2 :(得分:0)

  with tempTable(column1)
 as (
SELECT top(3) column1
FROM table1
ORDER BY column1DESC
) select top(1)column1 from tempTable

需要使用前3条记录

SELECT top(3) column1
    FROM table1
    ORDER BY column1DESC

答案 3 :(得分:0)

前3条记录很简单:

SELECT TOP 3 <column-name> FROM <table>

对于结果集中的第三条记录,请尝试此页How do I get the nth row in a SQL Server table?

答案 4 :(得分:0)

从顶部获取第三行。

;with cte as
(
select *, 
       row_number() over (order by number) as rn
from master.dbo.spt_values

)
select * 
from cte
where rn=3

获得前三名

select top (100.0/3) percent * 
from master.dbo.spt_values
order by number

答案 5 :(得分:0)

您可以使用以下查询

SELECT *
 FROM(
     SELECT ROW_NUMBER() OVER (ORDER BY [OrderBYColumn]) AS SrNo, 
             <Column1,Column2,Column3 ....> 
     FROM 
         TableName
     )    AS TableAlias
 WHERE 
     SrNo=3

此外,对于from和to range,您可以使用

SELECT *
 FROM(
     SELECT ROW_NUMBER() OVER (ORDER BY [OrderBYColumn]) AS SrNo, 
             <Column1,Column2,Column3 ....> 
     FROM 
         TableName
     )    AS TableAlias
 WHERE 
SrNo between 3 and 5

答案 6 :(得分:0)

select * from table_name limit 3