SQL语句mysql分组统计并对每组分页

最近一个需求如图,为了避免多次查询数据库,就想着尽量写成一个SQL语句

最相关人物最相关联系方式最相关邮箱最相关地址
张三231890000000024本人13ewdwqe@qq.com12 广东省深圳市龙华区某某街道某某小区5栋160814
李四181890000000118本人32rwdwfsd@qq.com9湖南省长沙市某某县13
王五151890000000215本人32ewrwfsd@qq.com5  
赵六111890000000013非本人    
钱七5       

 

 

 

 

 

 

 

观察这个结果,发现数据有4中类型,每种类型都是现实每种类型的value的值,后面跟的是该value的统计数量,最终只显示数量最多的5条。

那么我们就需要对类型、value进行分组统计,但是联系方式处却还有要对关系(本人、非本人)进行分组统计,其他的三种不需要对该关系分组。

SQL如下:

-- 1、该层取指定的条数
		select
			t.columnDataType as dataType, t.columnDataValue as title, t.counts as count, t.columnDataRelation
		from (
			-- 2、该层对分的组进行排序
			select
				t3.columnDataType, t3.columnDataValue, t3.counts, t3.columnDataRelation,
				case when @columnDataType=t3.columnDataType then @num:=@num+1 else @num:=1 end rank,
				@columnDataType:=t3.columnDataType
			from (
				-- 3、该层对数据类型、值、关系分组:由于内层已经将不需要区分关系的统计的结果中关系字段统一为一个值了,所以这里再次分组统计后,每种(数据类型、值一致的数据)需要分组的结果行数就是关系的种类数,不需要分组的结果的行数就是一行
				select 
					tmp.columnDataType, tmp.columnDataValue,
					sum(tmp.counts) as counts,
					tmp.columnDataRelation
				from (
					-- 4、该层对数据类型、值、关系分组:需要对关系分组的那么关系的值保留,不需要对关系分组的那么关系字段统一输出为一个值
					select
						t2.column_data_type as columnDataType,t2.column_data_value as columnDataValue, 
						if(t2.column_data_type in ('0'), t2.column_data_relation, '') as columnDataRelation , count(1) as counts
					from 数据主表 t1
					inner join 数据明细表 t2 on t1.id_ep_model_analysis = t2.search_index
					where t1.search_flow_no='2aff02fccb79471cb34e17723a9d9b1d' and t2.column_data_relation in ('0', '1')
					group by t2.column_data_type, t2.column_data_value, t2.column_data_relation
				) tmp
				group by tmp.columnDataType, tmp.columnDataValue, tmp.columnDataRelation
			) t3select
			t.columnDataType as dataType, t.columnDataValue as title, t.counts as count,
			case when t.columnDataRelation='0' then '非本人' when t.columnDataRelation='1' then '本人' else '' end as flag
		from (
			select
				t3.columnDataType, t3.columnDataValue, t3.counts, t3.columnDataRelation,
				case when @columnDataType=t3.columnDataType then @num:=@num+1 else @num:=1 end rank,
				@columnDataType:=t3.columnDataType
			from (
				select
					tmp.columnDataType, tmp.columnDataValue, sum(tmp.counts) as counts, tmp.columnDataRelation
				from (
					select
						t2.column_data_type as columnDataType,t2.column_data_value as columnDataValue,
						if(t2.column_data_type in ('0'), t2.column_data_relation, '') as columnDataRelation , count(1) as counts
					from ep_model_analysis t1
					inner join ep_model_analy_dtl t2 on t1.search_index = t2.search_index
					where t1.search_flow_no='f8ff8b0764544e4f8c3d79f80c2a2f77'
					group by t2.column_data_type, t2.column_data_value, t2.column_data_relation
				) tmp
				group by tmp.columnDataType, tmp.columnDataValue, tmp.columnDataRelation
				order by tmp.columnDataType
			) t3, (SELECT @num:=0, @columnDataType:='') t4
			order by t3.columnDataType, t3.counts desc, t3.columnDataValue
		) t
		where t.rank <= 5
			order by t3.columnDataType, t3.counts
		) t
		where t.rank <=5
		;

结果示例如下:

-- 上面的SQL结果:
  /*
  例如:
   4查询之后的结果为:
    columnDataType  columnDataValue  columnDataRelation  counts
    1                             数据1                        0                                  11
    1                             数据1                        1                                  3
    1                             数据3                        0                                  13
    1                             数据3                        1                                  4
    1                             数据4                        0                                  31
    1                             数据4                        1                                  14
    1                             数据5                        0                                  23
    1                             数据5                        1                                  8
    2                             数据2                                                           12
    2                             数据2                                                           5
    2                             数据6                                                           121
    2                             数据6                                                           51
    2                             数据7                                                           124
    2                             数据7                                                           54
    2                             数据8                                                           23
    2                             数据8                                                           5
    2                             数据9                                                           34
    2                             数据9                                                           5
    2                             数据10                                                         15
    2                             数据10                                                         6
    2                             数据11                                                         18
    2                             数据11                                                         5
   
   3查询之后的结果为:
    columnDataType  columnDataValue  columnDataRelation  counts
    1                               数据1                              0                      11
    1                               数据1                              1                      3
    1                               数据3                              0                      13
    1                               数据3                              1                      4
    1                               数据4                              0                      31
    1                               数据4                              1                      14
    1                               数据5                              0                      23
    1                               数据5                              1                      8
    2                               数据2                                                     17
    2                               数据6                                                     172
    2                               数据7                                                     178
    2                               数据8                                                     28
    2                               数据9                                                     39
    2                               数据10                                                   21
    2                               数据11                                                   23
   
   2查询之后的结果为:
    columnDataType  columnDataValue  columnDataRelation  counts  rank
    1                             数据4                            0                          31           1
    1                             数据5                            0                          23           2
    1                             数据4                            1                          14           3
    1                             数据3                            0                          13           4
    1                             数据1                            0                          11           5
    1                             数据5                            1                          8             6
    1                             数据3                            1                          4             7
    1                             数据1                            1                          3             8
    2                             数据7                                                      178           1
    2                             数据6                                                      172           2
    2                             数据9                                                      39             3
    2                             数据8                                                      28             4
    2                             数据11                                                    23             5
    2                             数据10                                                    21             6
    2                             数据2                                                      17             7
   
   1查询之后的结果为:
    columnDataType  columnDataValue  columnDataRelation  counts
    1                             数据4                            0                          31  
    1                             数据5                            0                          23  
    1                             数据4                            1                          14  
    1                             数据3                            0                          13  
    1                             数据1                            0                          11  
    2                             数据7                                                      178  
    2                             数据6                                                      172  
    2                             数据9                                                      39  
    2                             数据8                                                      28  
    2                             数据11                                                    23 
   
   最终显示的结果就是:
    1、columnDataType=1的需要显示关系,取总数最多的五条,
    2、columnDataType=2的不显示关系,取总数最多的五条
  */

 

 

 

 

 

转载于:https://my.oschina.net/u/3308528/blog/3100981

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值