zoukankan      html  css  js  c++  java
  • 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
    */


  • 相关阅读:
    servlet-01
    JavaWeb——文件上传和下载
    tomcat 7.0.94 下载安装步骤
    java 中 contains() containsKey() containsvalue() 使用
    java通过Runtime和Process类调用外部命令
    build.xml编译报错Specified VM install not found: type Standard VM, name jdk1.7.0_45
    微信小程序样式旋转
    微信小程序轮播图组件 swiper,swiper-item及轮播图片自适应
    HTTPS请求
    ztree插件的使用
  • 原文地址:https://www.cnblogs.com/Roy_88/p/5463054.html
Copyright © 2011-2022 走看看