SQL2008避免出错(聚合函数+Over用法)

--返回出错
with CTEOrders as
	(select cast(1 as int) as OrderID, cast('3/1/2012' as date) as OrderDate, cast(10.00 as money) as OrderAmt, 'Joe' as CustomerName
	union select 2, '3/1/2012', 11.00, 'Sam'
	union select 3, '3/2/2012', 10.00, 'Beth'
	union select 4, '3/2/2012', 15.00, 'Joe'
	union select 5, '3/2/2012', 17.00, 'Sam'
	union select 6, '3/3/2012', 12.00, 'Joe'
	union select 7, '3/4/2012', 10.00, 'Beth'
	union select 8, '3/4/2012', 18.00, 'Sam'
	union select 9, '3/4/2012', 12.00, 'Joe'
	union select 10, '3/4/2012', 11.00, 'Beth'
	union select 11, '3/5/2012', 14.00, 'Sam'
	union select 12, '3/6/2012', 17.00, 'Beth'
	union select 13, '3/6/2012', 19.00, 'Joe'
	union select 14, '3/7/2012', 13.00, 'Beth'
	union select 15, '3/7/2012', 16.00, 'Sam'
	)
select sum(OrderAmt), sum(OrderAmt) over()
   from CTEOrders
   group by CustomerName
 /*
 消息 155,级别 15,状态 1,第 24 行
'CTEOrders' 不是可以识别的 GROUP BY 选项。
*/  
--这样用避免出错
;with CTEOrders as
	(select cast(1 as int) as OrderID, cast('3/1/2012' as date) as OrderDate, cast(10.00 as money) as OrderAmt, 'Joe' as CustomerName
	union select 2, '3/1/2012', 11.00, 'Sam'
	union select 3, '3/2/2012', 10.00, 'Beth'
	union select 4, '3/2/2012', 15.00, 'Joe'
	union select 5, '3/2/2012', 17.00, 'Sam'
	union select 6, '3/3/2012', 12.00, 'Joe'
	union select 7, '3/4/2012', 10.00, 'Beth'
	union select 8, '3/4/2012', 18.00, 'Sam'
	union select 9, '3/4/2012', 12.00, 'Joe'
	union select 10, '3/4/2012', 11.00, 'Beth'
	union select 11, '3/5/2012', 14.00, 'Sam'
	union select 12, '3/6/2012', 17.00, 'Beth'
	union select 13, '3/6/2012', 19.00, 'Joe'
	union select 14, '3/7/2012', 13.00, 'Beth'
	union select 15, '3/7/2012', 16.00, 'Sam'
	)
select sum(OrderAmt), sum(sum(OrderAmt)) over()
   from CTEOrders
   group by CustomerName
;
/*
61.0000	205.0000
68.0000	205.0000
76.0000	205.0000
*/


转载于:https://www.cnblogs.com/Roy_88/p/5463054.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值