Search code examples
sql-servert-sqlwindow-functions

strange ssms results about t-sql SUM func, couldn't figure out


I am learning t-sql window function when found something strange, here is the normal code, very simple. enter image description here

then I tried delete the window frame clause and got the same results. enter image description here

anyone can explain why the result of the second one, thanks.


Solution

  • The second query returns the same results, because "range between unbounded preceding and current row" is the default frame, as you can see in the documentation and there are no duplicated rows in your table (i.e. multiple rows with the same empid and ordermonth).

    If ROWS/RANGE is not specified but ORDER BY is specified, RANGE UNBOUNDED PRECEDING AND CURRENT ROW is used as default for window frame.