MySQL转换ROW_NUMBER()超过分区

时间:2017-10-22 00:21:05

标签: mysql sql-server

我正在尝试将MSSQL数据库转换为MySQL,我的版本是5.7。我遇到了障碍。

SELECT orderid, invs.[InvoiceID],  invs.[InvoiceDate],
invs.[InvoiceNumber], invs.[HasClientPaid],
ROW_NUMBER() OVER (PARTITION by orderid,invs.invoicenumber,HasClientpaid ORDER BY orderid) AS DistNum 
FROM InvoiceLineItems Ilt 
JOIN Invoices Invs ON Ilt.InvoiceID= invs.InvoiceID

非常感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:0)

MySQL将开始支持窗口函数,例如版本8.x中的SELECT @row_num :=IF(@prev_value=concat_ws('',orderid, invs.invoicenumber, HasClientpaid),@row_num+1,1)AS RowNumber , orderid , invs.[InvoiceID] , invs.[InvoiceDate] , invs.[InvoiceNumber] , invs.[HasClientPaid] , @prev_value := concat_ws('',orderid, invs.invoicenumber, HasClientpaid) FROM InvoiceLineItems Ilt JOIN Invoices Invs ON Ilt.InvoiceID = invs.InvoiceID CROSS JOIN (SELECT @row_num :=1, @prev_value :=0) vars ORDER BY orderid, invs.invoicenumber, HasClientpaid ; (尚未生成就绪),直到那时使用@variables是一种模拟效果的技术:

orderid, invs.invoicenumber, HasClientpaid

您需要连接3个字段ORDER BY以模仿原始分区,并且顺序也需要由这3个列组成。 Collector对于此工作至关重要,如果您需要其他最终排序,请将上述内容用作子查询。