zoukankan      html  css  js  c++  java
  • mssql2005 cube和grouping的分组统计

    SQL语句练习使用cube和grouping的分组统计。

    代码
    ------------------------------------------------------------------------
    --
    author:jc_liumangtu(【DBA】小七)
    --
    date: 2010-03-05 17:00:41
    --
    version:
    --
    Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)
    --
    Oct 14 2005 00:33:37
    --
    Copyright (c) 1988-2005 Microsoft Corporation
    --
    Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --

    --
    ----------------------------------------------------------------------

    use test
    set nocount on
    if object_id('test2','U') is not null
    drop table test2
    go
    create table test2
    (
    area
    char(10),
    [month] char(10),
    total_sale
    int
    )

    insert into test2
    select '广州','1月',2000 union all
    select '广州','2月',2500 union all
    select '深圳','1月',1000 union all
    select '深圳','2月',2000
    go
    select * from test2

    select (case when grouping(area)=1 and grouping([month])=1 then '所有地区'
    when grouping(area)=1 and grouping([month])=0 then '月份小记' else area end) area
    ,
    isnull([month],'总计') [month],sum(total_sale) as [sum(total_sale)] from test2 group by area,[month] with cube

    area
    month total_sale
    ---------- ---------- -----------
    广州 1月 2000
    广州 2月
    2500
    深圳 1月
    1000
    深圳 2月
    2000

    area
    month sum(total_sale)
    ---------- ---------- ---------------
    广州 1月 2000
    广州 2月
    2500
    广州 总计
    4500
    深圳 1月
    1000
    深圳 2月
    2000
    深圳 总计
    3000
    所有地区 总计
    7500
    月份小记 1月
    3000
    月份小记 2月
    4500



  • 相关阅读:
    shell中对于命令的搜寻顺序
    在shell中运行以不同方式运行脚本
    shell中的type命令
    shell中的数组
    shell中的循环语句
    shell中的case表达式
    双方括号
    34. Win7_x64安装oracle11g出现DIM-00019
    33. 完全卸载oracle11g步骤
    32. 安装oracle11g时,先决条件一直失败的解决方法
  • 原文地址:https://www.cnblogs.com/dba_xiaoqi/p/1852549.html
Copyright © 2011-2022 走看看