有的时候我们要从数据库里把数据组织成树结构再展现到页面上

像下面这样

今天我们用Group 和Grouping实现它,并总结一下它俩。

先看一下概念,再用代码一点一点去理解它们,最后我会给出完整的代码

Group By : 语句用于结合合计函数,根据一个或多个列对结果集进行分组。

Grouping :指示是否聚合 GROUP BY 列表中的指定列表达式。 在结果集中,如果 GROUPING 返回 1 则指示聚合;

返回 0 则指示不聚合。 如果指定了 GROUP BY,则 GROUPING 只能用在 SELECT <select> 列表、HAVING 和 ORDER BY 子句中。

ROLLUP :生成简单的 GROUP BY 聚合行以及小计行或超聚合行,还生成一个总计行。

让我们先建一个数据库,并添加一些数据

use master
go
if exists(select 1 from sysdatabases where name ='MyGroupDB')
ALTER DATABASE MyGroupDB SET SINGLE_USER with ROLLBACK IMMEDIATE
drop database MyGroupDB
go create database MyGroupDB
go
use MyGroupDB
go create Table Category
(
Category_ID int identity(1,1),
Category_Name varchar(100)
)
go
create Table Product
(
Product_ID int identity(1,1),
CategoryID int ,
Product_Name varchar(100)
)
go
insert into Category values('手机')
insert into Category values('台式机')
insert into Category values('数码相机')
go insert into Product values(1,'诺基亚')
insert into Product values(1,'三星')
insert into Product values(1,'苹果') insert into Product values(2,'HP')
insert into Product values(2,'IBM')
insert into Product values(2,'Dell') insert into Product values(3,'佳能')
insert into Product values(3,'尼康')
insert into Product values(3,'索尼')
go

看一下它们的数据

select *
from Category
left join Product on Category_ID = CategoryID

我们把它们用Group By分一下组

select Category_ID ,
Category_Name,
CategoryID,
Product_Name
from Category
left join Product on Category_ID = CategoryID
group by Category_ID ,CategoryID,Category_Name,Product_Name

我们看到这样和没有分组时展现的数据是一样的,让我们加上 ROLLUP 加上合计行

select Category_ID ,
Category_Name,
CategoryID,
Product_Name
from Category
left join Product on Category_ID = CategoryID
group by Category_ID ,CategoryID,Category_Name,Product_Name with rollup

我们看到了好多NULL数据,而且很有规律

这些规律我们可以用Grouping 看到

select Category_ID ,
GROUPING(Category_ID) as Category_IDGP,
Category_Name,
GROUPING(Category_Name) as Category_NameGP,
CategoryID,
GROUPING(CategoryID) as CategoryIDGP,
Product_Name,
GROUPING(Product_Name) as Product_NameGP
from Category
left join Product on Category_ID = CategoryID
group by Category_ID ,Category_Name,CategoryID,Product_Name with rollup

你会发现那些Null值就是Grouping 为1的时候

最后一行的合计是Categrory_ID的,我们不需要,CategoryID的合计我们也不需要我们要怎么去掉它们呢,在having 里

select Category_ID ,
GROUPING(Category_ID) as Category_IDGP,
CategoryID,
GROUPING(CategoryID) as CategoryIDGP,
Category_Name,
GROUPING(Category_Name) as Category_NameGP,
Product_Name,
GROUPING(Product_Name) as Product_NameGP
from Category
left join Product on Category_ID = CategoryID
group by Category_ID ,Category_Name,CategoryID,Product_Name with rollup
having GROUPING(Category_ID)=0 and GROUPING(CategoryID)=0

这样的结果 我们看到只有Product_Name的Grouping有为1 了

我们就是用它去实现这棵树

select
case GROUPING(Product_Name) when 1 then Category_Name else '' end as Category_Name,
case GROUPING(Product_Name) when 0 then Product_Name else '' end as Product_Name
from Category
left join Product on Category_ID = CategoryID
group by Category_ID ,Category_Name,CategoryID,Product_Name with rollup
having GROUPING(Category_ID)=0 and GROUPING(CategoryID)=0
order by Category_ID ,Product_Name

下面是完整的代码

use master
go
if exists(select 1 from sysdatabases where name ='MyGroupDB')
ALTER DATABASE MyGroupDB SET SINGLE_USER with ROLLBACK IMMEDIATE
drop database MyGroupDB
go create database MyGroupDB
go
use MyGroupDB
go create Table Category
(
Category_ID int identity(1,1),
Category_Name varchar(100)
)
go
create Table Product
(
Product_ID int identity(1,1),
CategoryID int ,
Product_Name varchar(100)
)
go
insert into Category values('手机')
insert into Category values('台式机')
insert into Category values('数码相机')
go insert into Product values(1,'诺基亚')
insert into Product values(1,'三星')
insert into Product values(1,'苹果') insert into Product values(2,'HP')
insert into Product values(2,'IBM')
insert into Product values(2,'Dell') insert into Product values(3,'佳能')
insert into Product values(3,'尼康')
insert into Product values(3,'索尼')
go select *
from Category
left join Product on Category_ID = CategoryID
-------------------------------------------------------- select Category_ID ,
Category_Name,
CategoryID,
Product_Name
from Category
left join Product on Category_ID = CategoryID
group by Category_ID ,CategoryID,Category_Name,Product_Name with rollup --------------------------------------------------------
select Category_ID ,
GROUPING(Category_ID) as Category_IDGP,
Category_Name,
GROUPING(Category_Name) as Category_NameGP,
CategoryID,
GROUPING(CategoryID) as CategoryIDGP,
Product_Name,
GROUPING(Product_Name) as Product_NameGP
from Category
left join Product on Category_ID = CategoryID
group by Category_ID ,Category_Name,CategoryID,Product_Name with rollup ----------------------
select Category_ID ,
GROUPING(Category_ID) as Category_IDGP,
CategoryID,
GROUPING(CategoryID) as CategoryIDGP,
Category_Name,
GROUPING(Category_Name) as Category_NameGP,
Product_Name,
GROUPING(Product_Name) as Product_NameGP
from Category
left join Product on Category_ID = CategoryID
group by Category_ID ,Category_Name,CategoryID,Product_Name with rollup
having GROUPING(Category_ID)=0 and GROUPING(CategoryID)=0 ------------------------- select
case GROUPING(Product_Name) when 1 then Category_Name else '' end as Category_Name,
case GROUPING(Product_Name) when 0 then Product_Name else '' end as Product_Name
from Category
left join Product on Category_ID = CategoryID
group by Category_ID ,Category_Name,CategoryID,Product_Name with rollup
having GROUPING(Category_ID)=0 and GROUPING(CategoryID)=0
order by Category_ID ,Product_Name

  

玩转数据库之 Group by Grouping的更多相关文章

  1. Group by Grouping

    玩转数据库之 Group by Grouping 有的时候我们要从数据库里把数据组织成树结构再展现到页面上 像下面这样 今天我们用Group 和Grouping实现它,并总结一下它俩. 先看一下概念, ...

  2. java中list集合的内容,如何使用像数据库中group by形式那样排序

    java中list集合的内容,如何使用像数据库中group by形式那样排序,比如:有一个 List<JavaBean> 他中包含了一些如下的内容JavaBean:name    mone ...

  3. Group By Grouping Sets

    Group by分组函数的自定义,与group by配合使用可更加灵活的对结果集进行分组,Grouping sets会对各个层级进行汇总,然后将各个层级的汇总值union all在一起,但却比单纯的g ...

  4. 数据库语法group by

    因为在做pgsql和mysql数据库时group by 有报错,但是在以前做mysql5.6的时候没有问题,虽然知道时违反了sql的语法问题,但是没有搞清楚什么原因,也找了不少资料,查找原因,在盆友的 ...

  5. 带你了解数据库中group by的用法

    前言 本章主要介绍数据库中group by的用法,也是我们在使用数据库时非常基础的一个知识点.并且也会涉及Join的使用,关于Join的用法,可以看我写的上一篇文章:带你了解数据库中JOIN的用法如有 ...

  6. Oracle PL/SQL之GROUP BY GROUPING SETS

    [转自] http://blog.csdn.net/t0nsha/article/details/6538838 使用GROUP BY GROUPING SETS相当于把需要GROUP的集合用UNIO ...

  7. GROUP BY GROUPING SETS 示例

    --建表 create table TEst1 ( ID ), co_CODE ), T_NAME ), Money INTEGER, P_code ) ); --插入基础数据 insert into ...

  8. group by <grouping sets(...) ><cube(...)>

    GROUP BY      GROUPING SETS() 后面将还会写学习 with cube,  with rollup,以及将它们转换为标准的GROUP BY的子句GROUP SET(), CU ...

  9. (4.6)sql2008中的group by grouping sets

    最近遇到一个情况,需要在内网系统中出一个统计报表.需要根据不同条件使用多个group by语句.需要将所有聚合的数据进行UNION操作来完成不同维度的统计查看. 直到发现在SQL SERVER 200 ...

随机推荐

  1. n&(n-1) n&(-n)

    n&(n-1)   n&(-n) n&(n-1)作用:将n的二进制表示中的最低位为1的改为0,先看一个简单的例子:n = 10100(二进制),则(n-1) = 10011 = ...

  2. INFORMATICA 的调优之 INFORMATICA SERVER TUNING

    INFORMATICA SERVER的调优我认为主要从两个级别来做,一个是MAPPING级别,一个是SESSION级别. 对于MAPPING级别的调优: 一  对MAPPING数据流向的优化: 1 控 ...

  3. EF+MVC+cod First项目性能优化总结

    1.EF:this.Configuration.UseDatabaseNullSemantics = true; //关闭数据库null比较行为 2.实体必填字段要加:[Required]属性,可定长 ...

  4. 输入三个整数,xyz,最终以从小到大的方式输出。利用中间变量

    <script>function bijiao(){ var x= parseFloat(document.getElementById("X").value); va ...

  5. 迷宫问题求解之“A*搜索”(二)

    摘要:在迷宫问题求解之"穷举+回溯"(一)这篇文章中采用"穷举+回溯"的思想,虽然能从迷宫的入口到出口找出一条简单路径,但是找出来的不是最优路径.因此本文采用A ...

  6. dom4j操作xml

    dom4j是一个Java的XML API,类似于jdom,用来读写XML文件.是一个非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源工具.可以在这个 ...

  7. 被废弃的 Thread.stop, Thread.suspend, Thread.resume 和Runtime.runFinalizersOnExit

    最近学习多线程的知识,看到API里说这些方法被废弃了,就查了一下原因 Thread.stop 这个方法会解除被加锁的对象的锁,因而可能造成这些对象处于不一致的状态,而且这个方法造成的ThreadDea ...

  8. CSS纯样式实现箭头、对话框等形状

    在使用第三方框架bootstrap的时候,本以为其是图片实现的小箭头,后来使用开发工具查看是用CSS来实现的,现记录如下: 之前都没仔细去观注过其原理,都是拿来使用,在实现小箭头之前需要了解下CSS的 ...

  9. Java 集合介绍

    1, Set :集合中对象不按特定的方式排序,并且没有重复对象,它有些实现类能对集合按特定方式排序 List :集合中对象按照索引位置排序,可以有重复对象,允许按照对象在集合中的索引位置检索独享,Li ...

  10. log4j加载方式导致的bae和sae部署异常

    这2天改在bae上部署代码,为了便于程序的功能测试,引入了log4j日志,但是问题来了..测试程序采用的是spring3.2.8框架搭建,web.xml引入日志代码为: <context-par ...