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



  • 相关阅读:
    luogu1525 [NOIp2011]关押罪犯 (并查集)
    luogu1084 [NOIp2012]疫情控制 (二分答案+倍增+dfs序)
    luogu1083 [NOIp2012]借教室 (二分答案+差分)
    bzoj4152 The Captain (dijkstra)
    luogu1081 [NOIp2012]开车旅行 (STL::multiset+倍增)
    suoi22 WRX知识树(dfs序)
    [HNOI2010]弹飞绵羊
    1596: [Usaco2008 Jan]电话网络
    [Tyvj Jan]青蛙跳荷叶
    [BZOJ1116] CLO
  • 原文地址:https://www.cnblogs.com/dba_xiaoqi/p/1852549.html
Copyright © 2011-2022 走看看