sql server - Inconsistent SUM when using OVER PARTITION in MSSQL -
on static table, i'm trying run following query in ssms (to latest transaction each user , sum dollar value):
select sum(nmoney) totalmoney ( select row_number() on (partition ngroup, nuser order dtransaction desc) seqnum , nmoney [mydb].[dbo].[mytable] ) mysubquery mysubquery.seqnum=1
this table 2,701,510 rows , nmoney column of type decimal(12,2). when run multiple times, varying result:
2317367341.75 2317370443.45 2317449819.62 2317360649.43 2317449819.62
what causing inconsistent result?
for floating point arithmetic order numbers added in can affect result.
but in case using decimal(12,2)
precise.
the issue duplicate values ngroup, nuser, dtransaction
row_number
not deterministic different runs can return different results.
to deterministic behaviour can add guaranteed unique column(s) end of order by
act tie breaker.
Comments
Post a Comment