简体   繁体   中英

How to use partition by and order by in over function?

I'm using SQL Server 2008 R2.

I'm trying to write a query showing the following:

select productname, unitprice,categoryid, sum(unitprice) 
over (partition by categoryid order by unitprice desc) As PriceSum
from Products

I want the outcome to order products by their unit price, while partitioning the products by their categories. I'm getting this error: Incorrect syntax near 'order'. What am I doing wrong?

You are nothing doing wrong. SQL Server 2008 does not support running aggregates with windowing function.

SQL Server 2012 finally has full support for windowing functions, including running aggregations. So if you really need this, you will need to upgrade.

See this SQLFiddle for SQL Server 2012: http://sqlfiddle.com/#!6/5303f/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM