SELECT TOP 10 * FROM table WHERE (dTime NOT IN (SELECT TOP 20 * FROM table ORDER BY dTime))
ORDER BY dTime字查询的表名与 table 重复时会出错:当没有用 EXISTS 引入子查询时,在选择列表中只能指定一个表达式。怎么解决?  我不想用游标 听说数据量大时效率不高    
谢谢!

解决方案 »

  1.   

    SELECT TOP 10 * FROM [table] WHERE dTime NOT IN (SELECT TOP 20 dtime FROM [table] ORDER BY dTime)
    ORDER BY dTime 
      

  2.   

    表如果重复的话,起个别名,如table1 a,a就是table1的别名,等同与table1。
    SELECT TOP 10 * FROM table1 a WHERE dTime NOT IN (SELECT TOP 20 dtime FROM table1 ORDER BY dTime)
    ORDER BY dTime 
      

  3.   

    create proc [dbo].[Proc_Person]
    (
    @pageSize int,
    @pageIndex int
    )
    as
    BEGIN
    declare @Sql nvarchar(Max)
    declare @sqlCount nvarchar(max)
    declare @TableName nvarchar(max)
    declare @Fields nvarchar(max)
    declare @where nvarchar(max)
    declare @orderBy nvarchar(max)
    declare @groupBy nvarchar(max)
    declare @BeginIndex int
    declare @EndIndex intset @TableName=N' person '
    set @Fields =N'id, name, age '
    set @where=N' age >12 '
    set @orderBy=N' id  '
    set @groupBy=N' '
    set @BeginIndex=(@pageIndex-1) * @pageSize
    set @EndIndex=@pageIndex * @pageSizeset @sql=N'
    BEGIN WITH NewTable As(select ' +@fields+ ', Row_Number() over(order by' +@orderBy+ ') as NewRowNumber
    from ' +@TableName+ ' where ' +@where+ ')
    select * from NewTable where NewRowNumber > ' +convert(nvarchar(255),@BeginIndex)+ ' AND NewRowNumber <= ' +convert(nvarchar(255),@EndIndex)+'
    End'
    set @sqlCount= N'
    BEGIN WITH otherTable As(
    select ' +@fields+ ',Row_Number() over(order by ' +@orderBy+ ') as RowNumber
    from ' +@TableName+ ' where ' +@where+ ')
    select Count(*) as Row from otherTable 
    End'Execute Sp_Executesql @Sql
    Execute Sp_ExecuteSql @sqlCount
    END
      

  4.   

    SELECT TOP 10 * FROM [table_name] WHERE dTime NOT IN (SELECT TOP 20 dtime FROM [table_name] ORDER BY dTime)
    ORDER BY dTime