如何为 SQL Server 2000 编写以下 sql 片段
有人可以帮助如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
DigbySwift 的回答提到 ROW_NUMBER 存在问题,您需要先解决这个问题。您可以在他的回答中使用这个想法,您还可以查看类似问题的答案,例如:
但是,还有另一个问题:SQL Server 2000 没有不支持 CTE(
WITH ... AS ...
)。要解决此问题,请通过删除
WITH
子句并将 CTE 的定义移动到使用它的位置,使用子选择而不是 CTE。例如,这个使用 CTE 的查询:
变成:
希望这足以让您开始。
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:
Becomes:
Hopefully that is enough to get you started.