为mysql/oracle重写SQL Server查询[关闭]

sqyvllje  于 2023-05-22  发布在  Oracle
关注(0)|答案(1)|浏览(175)

已关闭,此问题需要更focused。目前不接受答复。
**想改善这个问题吗?**更新问题,使其仅通过editing this post关注一个问题。

5小时前关闭
Improve this question
我需要为mySQL 8.0和Oracle 21c重写此SQL Server 2019查询

WITH yourTable AS (
    SELECT 1 AS Item, '2021-05-01' AS Date, 200 AS Price UNION ALL
    SELECT 1, '2021-06-11', 210 UNION ALL
    SELECT 1, '2021-06-27', 225 UNION ALL
    SELECT 1, '2021-08-01', 250 UNION ALL
    SELECT 2, '2021-02-10', 600 UNION ALL
    SELECT 2, '2021-04-21', 650 UNION ALL
    SELECT 2, '2021-06-17', 675 UNION ALL
    SELECT 2, '2021-07-23', 700
),
      WITH cte AS (
        SELECT *, DATEADD(day, -1, LEAD(Date, 1, GETDATE())
                                       OVER (PARTITION BY Item
                                             ORDER BY Date)) AS LastDate
        FROM yourTable
    ) SELECT Item, Date AS DateStart, Price, LastDate AS DateEnd
    FROM cte
    ORDER BY Item, Date;

from question
我尝试使用GET_DATE,CURDATE,但它不工作

13z8s7eq

13z8s7eq1#

对于mysql 8和Oracle 21c,使用INTERVAL代替DATEADD,使用CURRENT_DATE代替GETDATE()

WITH cte AS (
    SELECT  Item, Date_, Price, LEAD(CAST(Date_ AS Date), 1, CURRENT_DATE)
                                   OVER (PARTITION BY Item
                                         ORDER BY Date_)
                                - INTERVAL '1' DAY AS LastDate
    FROM yourTable
)
SELECT Item, Date_ AS DateStart, Price, LastDate AS DateEnd
FROM cte
ORDER BY Item, Date_;

Demo here

相关问题