2017年3月20日 星期一

不使用GROUP BY去sum

use Northwind;

select OrderID,CustomerID,Freight,(select sum(Freight) from Orders) As 合計 from Orders;

select OrderID,CustomerID,Freight,sum(Freight) over ( partition by 1 ) As 合計 from Orders;

備註:如果使用
select OrderID,CustomerID,Freight,sum(Freight) from Orders會有以下錯誤訊息
訊息 8120,層級 16,狀態 1,行 1
Column 'Orders.OrderID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

沒有留言:

張貼留言