Ok, so basically my DISTINCT useless with ROW_NUMBER() , I need to avoid this as it causes duplicate results (except for a unique amount of course!)
So, I am looking for a query that will work the same, but does not have duplicate rows, because num is unique:
WITH t AS ( SELECT DISTINCT *, ROW_NUMBER() OVER (ORDER BY Date) AS num FROM Original_Import LEFT JOIN eqcas.dbo.BASE_PROXY_VIEW_WITHTARGET ON ADName = Targetuser WHERE (BaseProxy = 'agmc' OR ADName = 'agmc') AND (Commited IS NULL OR Commited = 0) ) SELECT DISTINCT ID, num, ADName, Description_User, Description_Amex, Amount, Date FROM t WHERE (t.BaseProxy = 'agmc' OR t.ADName = 'agmc') AND num BETWEEN 0 AND 20 AND (Commited IS NULL OR Commited = 0) ORDER BY Date
Probably pretty trivial to fix, but, seeing that I'm not a SQL Server guy, I'm not used to these internal queries, etc.
Update: yes, num is used for swap.
source share