MySQL will commence to suport window functions such as row_number()
in version 8.x (not yet production ready as at Oct 29 2017), until then using @variables is a technique to mimic the effect:
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
;
You need to concatenate the 3 fields orderid, invs.invoicenumber, HasClientpaid
to mimic your original partitioning, and the ordering will need to be by those 3 columns as well. The ORDER BY
is essential for this to work, if you need some other final ordering use the above as a subquery.