gpt4 book ai didi

mysql - 将 MySQL 脚本移植到 MS SQL -> 同时进行数据分配和检索

转载 作者:行者123 更新时间:2023-11-29 09:56:00 25 4
gpt4 key购买 nike

我已经尝试了几个小时来完成这项工作,但我真的找不到办法。我有一个 MySQL 脚本,需要迁移到 MS SQL 2014,但是,我似乎无法使其工作。据我了解,最大的问题是 MySQL 允许这种指令:

SELECT @row_number = @row_number + 1, hora_int FROM table1;

您可以在检索数据的同时赋值,但 MS SQL 不会。

我有一个相对较大的脚本存在这个问题:

SELECT GLOBAL.year, GLOBAL.month, '1111111' as metric_id, GLOBAL.margin as metric_value FROM (SELECT A.year,
A.month,
CASE
WHEN A.month = 1 THEN (@csum_fdo := A.imp)
ELSE (@csum_fdo := @csum_fdo + A.imp) END as margin
FROM (SELECT Act.year, Act.month, Act.imp - Pas.imp as imp
FROM (SELECT year(tie.date) as year, month(tie.date) as month, sum(importe) as imp
FROM accounting con,
dim_time tie,
dim_account cta
WHERE con.account_date = tie.date
AND con.account = cta.nivel_10
AND cta.level_id = '2'
AND con.subtype_id <> 'O'
GROUP BY year(tie.date),
month(tie.date)) Act,
(SELECT year(tie.date) as year, month(tie.date) as month, sum(importe) * -1 as imp
FROM accounting con,
dim_time tie,
dim_account cta
WHERE con.account_date = tie.date
AND con.account = cta.nivel_10
AND cta.level_id = '3'
AND con.subtype_id <> 'O'
GROUP BY year(tie.date),
month(tie.date)) Pas
WHERE Act.year = Pas.year
AND Act.month = Pas.month) A,
(SELECT @csum_fdo := 0) E) GLOBAL

任何人都可以指出我必须做什么才能在 MS SQL 中重新创建代码吗?先谢谢大家了

最佳答案

在 SQL Server 和 (MyQL 8+) 中,您使用窗口函数。相当于:

SELECT @row_number = @row_number + 1, hora_int
FROM table1
ORDER BY col;

是:

SELECT ROW_NUMBER() OVER (ORDER BY col), hora_int
FROM table1;

如果您提供示例数据和期望的结果,就更容易找出您真正需要的功能。

关于mysql - 将 MySQL 脚本移植到 MS SQL -> 同时进行数据分配和检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53833131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com