如何为 SQL Server 2000 编写以下 sql 片段

发布于 2024-12-11 20:26:10 字数 966 浏览 0 评论 0原文

有人可以帮助如何在 SQL Server2000 中编写以下代码吗?谢谢

 WITH cte 
     AS (SELECT *, 
                ( Row_number() OVER(ORDER BY productid, transactionid, 
                  statusdate 
                  DESC) 
                   )AS 
                rownum 
         FROM   @table), 
     cte2 
     AS (SELECT cte.*, 
                ( CASE 
                    WHEN cte.status = Isnull((SELECT t.status 
                                              FROM   cte t 
                                              WHERE  t.rownum = ( cte.rownum + 1 
                                                                )), 
                                      '') 
                  THEN 1 
                    ELSE 0 
                  END )AS rownum2 
         FROM   cte) 
SELECT cte2.productid, 
       cte2.transactionid, 
       cte2.details, 
       cte2.status, 
       cte2.statusdate, 
       cte2.requestdate 
FROM   cte2 
WHERE  rownum2 = 0 

Can Someone please help to how to write the below code in SQL Server2000? Thanks

 WITH cte 
     AS (SELECT *, 
                ( Row_number() OVER(ORDER BY productid, transactionid, 
                  statusdate 
                  DESC) 
                   )AS 
                rownum 
         FROM   @table), 
     cte2 
     AS (SELECT cte.*, 
                ( CASE 
                    WHEN cte.status = Isnull((SELECT t.status 
                                              FROM   cte t 
                                              WHERE  t.rownum = ( cte.rownum + 1 
                                                                )), 
                                      '') 
                  THEN 1 
                    ELSE 0 
                  END )AS rownum2 
         FROM   cte) 
SELECT cte2.productid, 
       cte2.transactionid, 
       cte2.details, 
       cte2.status, 
       cte2.statusdate, 
       cte2.requestdate 
FROM   cte2 
WHERE  rownum2 = 0 

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

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

发布评论

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

评论(2

伪装你 2024-12-18 20:26:10

2000 年没有 OVER() 函数,因此您需要重新寻址/移动初始 cte 选择。您已经有一个表变量 @table,因此我将通过添加标识列来修改它以包含行号。

然后,您可以使用子查询从 cte2 中提取您需要的内容。

There is no OVER() function in 2000, so you will need to re-address/move the initial cte select. You already have a table variable @table, so I would modify this to include the row numbers, by adding a identity column.

You could then use a subquery to extract what you need from cte2.

渔村楼浪 2024-12-18 20:26:10

DigbySwift 的回答提到 ROW_NUMBER 存在问题,您需要先解决这个问题。您可以在他的回答中使用这个想法,您还可以查看类似问题的答案,例如:

但是,还有另一个问题:SQL Server 2000 没有不支持 CTE(WITH ... AS ...)。

要解决此问题,请通过删除 WITH 子句并将 CTE 的定义移动到使用它的位置,使用子选择而不是 CTE。

例如,这个使用 CTE 的查询:

WITH T1 AS (SELECT a,b,c FROM ...)
SELECT * FROM T1

变成:

SELECT *
FROM (
    SELECT a,b,c FROM ...
) AS T1

希望这足以让您开始。

DigbySwift's answer mentions that there is a problem with ROW_NUMBER and you need to solve that first. You can use the idea in his answer, and you could also look at answers to similar questions such as these:

However there's also another problem: SQL Server 2000 doesn't support CTEs (WITH ... AS ...).

To solve this use subselects instead of CTEs by removing the WITH clause and moving the definition of the CTE to the place(s) where it is used.

For example this query using CTEs:

WITH T1 AS (SELECT a,b,c FROM ...)
SELECT * FROM T1

Becomes:

SELECT *
FROM (
    SELECT a,b,c FROM ...
) AS T1

Hopefully that is enough to get you started.

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