SQL Server 2012:窗口中“顺序”附近的语法不正确

时间:2014-01-16 20:39:32

标签: sql sql-server tsql

这应该是一个直接的分区over / order by ...但是我今天已经得到了 dumb 并且看不到我邪恶方式的错误。

这是schema和sql:

create table #NoDiscountFleet
(fleet_number int,
 customer_key int,
 posyear int,
 posmonth int,
 sale_net_val  decimal(16,4),
 sale_tot_qty decimal(12,3),
 FirstDayOfMonth date,
 LastDayOfMonth date
);

select  fleet_number,
        lastdayofmonth,
        tot_fleet_net_sales = sum(sale_net_val) over (partition by fleet_number order by fleet_number,lastdayofmonth)
from #NoDiscountFleet
 group by fleet_number,lastdayofmonth
      order by fleet_number;

1 个答案:

答案 0 :(得分:1)

您不需要GROUP BY

SELECT  fleet_number,
        lastdayofmonth,
        tot_fleet_net_sales = SUM(sale_net_val) OVER (PARTITION BY fleet_number 
                                                      ORDER BY lastdayofmonth)
FROM #NoDiscountFleet
ORDER BY fleet_number;