ROW_NUMBER()列无效并尝试加快查询帮助

时间:2009-03-04 16:36:49

标签: sql-server tsql

我正在尝试使用ROW_NUMBER函数,但我遇到了两个问题。 1)因为我按降序排序,所以rowid不是从1开始,例如从41开始。 2)当我尝试将rowid恢复到42和52之间时,它表示rowid是一个无效的列。这是一个调低的查询,其中一些额外的列被取出,因此它更具可读性:

SELECT ROW_NUMBER() OVER (ORDER BY column1) AS rowid,
hrl.column0
(SELECT top 1 bus from hc WHERE column2 = hc.column2) AS c,
'Site' as Based,
column1, column2, column3, column4,
CASE WHEN status = '1' Then 'Active'
     WHEN status = '2' Then 'Canceled'
ELSE
   'Unknown'
END as column5,
column6,
(select count(*) from hrl where column1 = hrl.column1) as [cnt]
FROM hrl
INNER JOIN hc on hc.column2 = hrl.column2
INNER JOIN rsc on rsc.column3 = hrl.column3
LEFT OUTER JOIN  gmt on gmt.[column7] = hrl.[column7]
where bus LIKE '%test%'
ORDER BY [cnt] desc,rowid
and  rowid >= 1 AND rowid <= 10 -- Says rowid is invalid

这是我想要的数据

RowID Business Count

1     Green    50
2     Red      30
3     Blue     20

这是我得到的:

RowID Business Count

3     Blue     20
2     Red      30
1     Green    50

2 个答案:

答案 0 :(得分:3)

将它放入子查询:

SELECT *
FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY column1) AS rowid,
    hrl.column0,
    (SELECT top 1 bus from hc WHERE column2 = hc.column2) AS c,
    'Site' as Based,
    column1, column2, column3, column4,
    CASE WHEN status = '1' Then 'Active'
         WHEN status = '2' Then 'Canceled'
    ELSE
       'Unknown'
    END as column5,
    column6,
    (select count(*) from hrl where column1 = hrl.column1) as [cnt]
    FROM hrl
    INNER JOIN hc on hc.column2 = hrl.column2
    INNER JOIN rsc on rsc.column3 = hrl.column3
    LEFT OUTER JOIN  gmt on gmt.[column7] = hrl.[column7]
    where bus LIKE '%test%'
) sq
WHERE rowid >= 1 AND rowid <= 10
ORDER BY [cnt] DESC

如果您只想按TOP 10 column1排序[cnt]行,则可以使用:

SELECT *
FROM (
    SELECT TOP 10
           hrl.column0,
           (SELECT top 1 bus from hc WHERE column2 = hc.column2) AS c,
           'Site' as Based,
           column1, column2, column3, column4,
           CASE WHEN status = '1' Then 'Active'
                WHEN status = '2' Then 'Canceled'
                ELSE 'Unknown'
           END as column5,
           column6,
           (select count(*) from hrl where column1 = hrl.column1) as [cnt]
    FROM   hrl
    INNER JOIN hc on hc.column2 = hrl.column2
    INNER JOIN rsc on rsc.column3 = hrl.column3
    LEFT OUTER JOIN  gmt on gmt.[column7] = hrl.[column7]
    WHERE  bus LIKE '%test%'
    ORDER BY column1
) sq
ORDER BY [cnt] DESC

或者,如果您只需要ORDER BY [cnt] DESC,请尝试以下操作:

SELECT TOP 10
       hrl.column0,
       (SELECT top 1 bus from hc WHERE column2 = hc.column2) AS c,
       'Site' as Based,
       column1, column2, column3, column4,
       CASE WHEN status = '1' Then 'Active'
            WHEN status = '2' Then 'Canceled'
            ELSE 'Unknown'
       END as column5,
       column6,
       (select count(*) from hrl where column1 = hrl.column1) as [cnt]
FROM   hrl
INNER JOIN hc on hc.column2 = hrl.column2
INNER JOIN rsc on rsc.column3 = hrl.column3
LEFT OUTER JOIN  gmt on gmt.[column7] = hrl.[column7]
WHERE  bus LIKE '%test%'
ORDER BY [cnt] DESC

答案 1 :(得分:0)

将未分类的数据包裹为CTE(公用表表达式)

;with NotSorted as (
    SELECT  ROW_NUMBER() OVER (ORDER BY column1) AS rowid, 
        hrl.column0, 
        (SELECT top 1 bus from hc WHERE column2 = hc.column2) AS c,
        'Site' as Based, column1, column2, column3, column4,
        CASE WHEN status = '1' Then 'Active'
             WHEN status = '2' Then 'Canceled'
             ELSE 'Unknown'
        END as column5, column6,
        (select count(*) from hrl where column1 = hrl.column1) as [cnt]
    FROM    hrl
            INNER JOIN hc on hc.column2 = hrl.column2
            INNER JOIN rsc on rsc.column3 = hrl.column3
            LEFT OUTER JOIN  gmt on gmt.[column7] = hrl.[column7]
    where   bus LIKE '%test%'
)
select  *
from    NotSorted
ORDER BY [cnt] desc, rowid and  rowid >= 1 AND rowid <= 10

至于,

  

和rowid&gt; = 1 AND rowid&lt; = 10 - 表示rowid无效

您无法直接访问计算别名,您通常会将计算列放入子查询或CTE