这个查询有什么问题? 无法让 ROW_NUMBER() 工作

发布于 2024-07-09 22:21:54 字数 1010 浏览 11 评论 0原文

SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY hrl.Frn) as Row,
        hrl.unq, hrl.LcnsId, hc.Business,hc.Name,hc.Phone,
        hrl.Frn,hrl.CallSign, hrl.gsamarkettypeid, 
        gmt.[Market Type Code] + ' - ' + gmt.gsamarkettype,
        hrl.gsalatitude,hrl.gsalongitude,
        rsc.RadioServiceCode + ' - ' + rsc.RadioService, 
                    GrantDt, ExpirationDt, EffectiveDt, 
        CancellationDt
        FROM dbo.sbi_f_HldrRgstrtnLcns hrl
        INNER JOIN dbo.sbi_f_HldrCntcts hc on 
                    hc.CallSign = hrl.CallSign
        INNER JOIN dbo.sbi_l_radioservicecodes rsc on 
                    rsc.radioservicecodeid = hrl.radioservicecodeid
        LEFT OUTER JOIN dbo.sbi_l_GSAMarketTypes gmt on 
                    gmt.GSAMarketTypeId = hrl.GSAMarketTypeId
        WHERE hc.Entity_Type = 'L'  AND hrl.LicenseStatusId IN (1)
        and Row >=1 and Row <= 20) -- The error occurs here,
                    -- it says incorrect syntax near )
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY hrl.Frn) as Row,
        hrl.unq, hrl.LcnsId, hc.Business,hc.Name,hc.Phone,
        hrl.Frn,hrl.CallSign, hrl.gsamarkettypeid, 
        gmt.[Market Type Code] + ' - ' + gmt.gsamarkettype,
        hrl.gsalatitude,hrl.gsalongitude,
        rsc.RadioServiceCode + ' - ' + rsc.RadioService, 
                    GrantDt, ExpirationDt, EffectiveDt, 
        CancellationDt
        FROM dbo.sbi_f_HldrRgstrtnLcns hrl
        INNER JOIN dbo.sbi_f_HldrCntcts hc on 
                    hc.CallSign = hrl.CallSign
        INNER JOIN dbo.sbi_l_radioservicecodes rsc on 
                    rsc.radioservicecodeid = hrl.radioservicecodeid
        LEFT OUTER JOIN dbo.sbi_l_GSAMarketTypes gmt on 
                    gmt.GSAMarketTypeId = hrl.GSAMarketTypeId
        WHERE hc.Entity_Type = 'L'  AND hrl.LicenseStatusId IN (1)
        and Row >=1 and Row <= 20) -- The error occurs here,
                    -- it says incorrect syntax near )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抱猫软卧 2024-07-16 22:21:55

将您的行条件移动到外部选择中

SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY hrl.Frn) as Row,
     ...
               WHERE hc.Entity_Type = 'L'  AND hrl.LicenseStatusId IN (1)
              ) T
WHERE T.Row >=1 and T.Row <= 20)

Move your Row criteria into the outer select

SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY hrl.Frn) as Row,
     ...
               WHERE hc.Entity_Type = 'L'  AND hrl.LicenseStatusId IN (1)
              ) T
WHERE T.Row >=1 and T.Row <= 20)
梦毁影碎の 2024-07-16 22:21:55

您可以使用 CTE 来做到这一点:

WITH NumberedRows AS
(
    SELECT 
        ROW_NUMBER() OVER (ORDER BY hrn.Frl) AS RowNum,
        ...
    WHERE 
        hc.Entity_Type = 'L'
        AND hrl.LicenseStatusId IN (1)
)
SELECT
    *
FROM
    NumberedRows
WHERE
    RowNum <= 20

You could do this using a CTE:

WITH NumberedRows AS
(
    SELECT 
        ROW_NUMBER() OVER (ORDER BY hrn.Frl) AS RowNum,
        ...
    WHERE 
        hc.Entity_Type = 'L'
        AND hrl.LicenseStatusId IN (1)
)
SELECT
    *
FROM
    NumberedRows
WHERE
    RowNum <= 20
奶茶白久 2024-07-16 22:21:55

检查您使用的是哪个版本的 sql server 数据库。 row_number 是从 sql server 2005 开始添加的。 它不支持sql server 2000

check which version of sql server database are you using. The row_number is added from sql server 2005 onwards. It will not support sql server 2000

清泪尽 2024-07-16 22:21:55

不能对 where 子句中的列使用别名,因为 where 子句是在 Select 子句之前处理的。

编辑:

您也可以只在 where 子句中添加子查询,

Where ((SELECT ROW_NUMBER() OVER (ORDER BY hrl.Frn)) < 20,

或者根本不使用 Row_Number,

Where (Select Count(*) 
From dbo.sbi_f_HldrRgstrtnLcns 
Where Frn < hrl.Frn) < 20

You can't use an alias for a column in a where clause, because the where clause is processed before the Select Clause.

EDITED:

You could also just add the subquery in the where clause

Where ((SELECT ROW_NUMBER() OVER (ORDER BY hrl.Frn)) < 20,

or without usingt Row_Number at all,

Where (Select Count(*) 
From dbo.sbi_f_HldrRgstrtnLcns 
Where Frn < hrl.Frn) < 20
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文