2017-10-22 79 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) –

相關問題