2017-10-22 54 views
2

我试图将MSSQL数据库转换为MySQL,我的版本是5.7。我遇到了障碍。MySQL转换ROW_NUMBER()OVER分区

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 

任何帮助将不胜感激。谢谢

回答

0

MySQL将开始向询问服务窗口功能,如在8.x版本row_number()(尚未投入生产),在此之前使用@variables是模仿的效果的技术:

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 
; 

您需要连接3个字段orderid, invs.invoicenumber, HasClientpaid以模仿您的原始分区,并且排序也需要在这3列中。 ORDER BY是必不可少的这个工作,如果你需要一些其他最终订购使用上述作为子查询。

+0

你的问题现在解决了吗?你仍然有关于这个答案的问题吗?要接受答案“[**点击Tick **](https://ibb.co/ikqyO6)”以获取更多信息,请参阅[help/accepting](https://stackoverflow.com/help/someone-answers) –

相关问题