###### Welcome to itblog

Every Day In Every Way I Am Getting Better And Better~

###### 数据分组问题

F1       F2
-----------------
A         1
A         1
A         1
A         2
A         1
B         1
B         1
B         1

F1        F2
-----------------
A         1
A         1
A         1
A         2
A         1
Total A: 6
B         1
B         1
B         1
Total B: 3

--数据
declare @t table (F1 varchar(10),F2 int)
insert @t select 'A',1
union all select 'A',1
union all select 'A',1
union all select 'A',2
union all select 'A',1
union all select 'B',1
union all select 'B',1
union all select 'B',1
union all select 'C',1
union all select 'C',2
union all select 'C',1
union all select 'C',3

--语句1.不显示最后一行（如果不要的话）

declare @count int
declare @s varchar(8000)
select id=identity(int,1,1), * into # from @t
set @count=@@rowcount+(select count(distinct(F1)) from #)
select @count
select  @s=(
'select top '+rtrim(@count)+'
(
case isnumeric(isnull(rtrim(id),''Total:'')) when 1 then ''''
when 0  then (isnull(rtrim(id),''Total:'')) end
)+isnull(F1,''All'') as F1
,sum(F2) as F2
from #
group by F1,id with rollup
')
exec (@s)

--语句2，显示最后一行
select
(
case isnumeric(isnull(rtrim(id),'Total:')) when 1 then ''
when 0  then (isnull(rtrim(id),'Total:')) end
)+isnull(F1,'All') as F1
,sum(F2) as F2
from #
group by F1,id with rollup

--破弃测试环境
drop table #

/*

F1                     F2
---------------------- -----------
A                      1
A                      1
A                      1
A                      2
A                      1
Total:A                6
B                      1
B                      1
B                      1
Total:B                3
C                      1
C                      2
C                      1
C                      3
Total:C                7

F1                     F2
---------------------- -----------
A                      1
A                      1
A                      1
A                      2
A                      1
Total:A                6
B                      1
B                      1
B                      1
Total:B                3
C                      1
C                      2
C                      1
C                      3
Total:C                7
Total:All              16

*/

#### 集合的最优分组问题

2017-01-18 16:49:55

#### 使用JqGrid列表数据分组显示问题的解决方法

2015-06-13 21:34:05

#### NOIP2007:纪念品分组

2016-02-04 15:45:43

#### 算法-（分组情况）

2018-03-21 10:55:59

#### C:动态规划之分组问题

2016-11-18 21:17:20

#### js根据json数据中的某一个属性来给数据分组

2017-01-10 15:28:05

#### pandas 数据分组运算

2016-12-16 13:26:26

#### R语言学习笔记 —— 数据的分组处理

2014-04-12 11:12:31

#### Excel在统计分析中的应用—第二章—描述性统计-分组数据的中位数的求解方法（组离散数据）

2018-01-08 18:58:00

#### PHP之数据分组

2016-02-17 17:06:02