SQL Server BETWEEN和OR语句

时间:2013-12-30 06:47:11

标签: sql-server-2008-r2

我想在SQL Server中使用2个子句,如下所示:

SELECT ROW_NUMBER() OVER (ORDER BY date_time ASC) AS RowNumber,
       <column_name1>,
       <column_name2>,
       <column_name3>, 
       date_time 
from <table name>
where <column name1> = 'assdf'  
   or RowNumber between 1 and 10

但这会产生错误。这个查询出了什么问题?

4 个答案:

答案 0 :(得分:0)

尝试使用CTE expressions

with cte as
       (    
         SELECT ROW_NUMBER() OVER(ORDER BY date_time ASC)AS RowNumber, 
                <column_name1>,
                <column_name2>,
                <column_name3>, 
                date_time from <table name> 
       )
select * from cte where <column name1> = 'assdf' or RowNumber between 1 and 10

答案 1 :(得分:0)

with Temp as
(
SELECT ROW_NUMBER() OVER(ORDER BY date_time ASC)AS RN, 
       <column_name1>,
       <column_name2>,
       <column_name3>, 
       date_time 
       from <table name>
       where  <column name1> = 'assdf'
)
SELECT * FROM Temp where RN BETWEEN 1 and 10

SELECT  <column_name1>, 
        <column_name2>,
        <column_name3>,
        date_time FROM
        (
       SELECT ROW_NUMBER() OVER(ORDER BY date_time ASC)AS RN,
             <column_name1>,   
             <column_name2>,
             <column_name3>,
             date_time 
             from <table name>
             where  <column name1> = 'assdf'
        ) As S

WHERE RN BETWEEN 1 and 10

答案 2 :(得分:0)

您尝试在同一查询中引用RowNumber列,而是在外部查询中引用它。试试这个:

select * from 
(
SELECT ROW_NUMBER() OVER (ORDER BY date_time ASC) AS RowNumber,
      <column_name1>,
      <column_name2>,
      <column_name3>, 
      date_time
      from <table name>
) tab
 where <column name1> = 'assdf' or
       RowNumber between 1 and 10

答案 3 :(得分:0)

这将有助于

select * from 
   ( 
     SELECT ROW_NUMBER() OVER(ORDER BY date_time ASC)AS RN,
           <column_name1>,
           <column_name2>,
           <column_name3>,
           date_time
           from <table name>
           where  <column name1> = 'assdf'
   ) 
 where RN between 1 and 10