简体   繁体   English

如何在SQL中获取ROW_NUMBER()?

[英]How to get ROW_NUMBER() in SQL?

I tried to get the row number using ROW_NUMBER() but it shows the following error: 我尝试使用ROW_NUMBER()获取行号,但显示以下错误:

can't format message 13:896 -- message file C:\\WINDOWS\\firebird.msg not found. 无法格式化消息13:896-找不到消息文件C:\\ WINDOWS \\ firebird.msg。 Dynamic SQL Error. 动态SQL错误。 SQL error code = -104. SQL错误代码= -104。 Token unknown - line 2, column 66. 令牌未知-第2行,第66列。

Here is my code: 这是我的代码:

SELECT  avg(CSIDTL.RATING) ,SVD.SVCADVISORNAME, ROW_NUMBER() OVER(ORDER BY avg(CSIDTL.RATING) )
        FROM T_APPT_BOOKING_MSTR MSTR ,T_APPT_CSI_SURVEY CSI,T_APPT_CSI_SURVEY_DTL CSIDTL,
        T_SVC_SVCADVISOR_MASTER SVD
        WHERE MSTR.APPTBKID = CSI.APPTBKID
        AND CSI.CSI_SURVERYID = CSIDTL.CSI_SURVERYID
        AND SVD.SVCADVISORID = MSTR.SVCADVISORID
        AND CSI.FEEDBACK_STATUS = 'Y'
        AND CSIDTL.question ='Service Advisor'
        GROUP BY SVD.SVCADVISORNAME
        ORDER by avg(CSIDTL.RATING)

The ROW_NUMBER() function was introduced with Firebird 3.0, released just few days ago. 几天前发布的Firebird 3.0引入了ROW_NUMBER()函数。 See release notes, chapter Window (Analytical) Functions for exact syntax. 有关确切的语法,请参见发行说明的“ 窗口(分析)函数”一章。 The error you get suggest you're using an older version of Firebird which doesn't have this feature. 您收到的错误提示您使用的是不具备此功能的旧版Firebird。

I use this in Firebird 2.5 我在Firebird 2.5中使用它

Reference: http://www.firebirdfaq.org/faq343/ 参考: http//www.firebirdfaq.org/faq343/

SELECT rdb$get_context('USER_TRANSACTION', 'row#') as row_number, DUMMY, A.*
FROM your_table A
CROSS JOIN
(SELECT rdb$set_context('USER_TRANSACTION', 'row#',
COALESCE(CAST(rdb$get_context('USER_TRANSACTION', 'row#') AS INTEGER), 0) + 1) AS dummy
FROM rdb$database) dummy

Other alternative in Firebird 2.5 is use of generators Firebird 2.5中的其他替代方法是使用发电机

CREATE GENERATOR tmp$rn;
UPDATE my_table t SET t.id_field = (SELECT FIRST 1 NEXT VALUE FOR tmp$rn AS "row_number"
FROM my_table ORDER BY another_field1 DESC, another_field2 DESC);
DROP GENERATOR tmp$rn;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM