
有这样一个表
|id |col1|col2|col3|col4| |---|----|----|--- |----| | 1 | 1 | 1 | 1 | 1 | | 2 | 1 | 1 | 1 | 2 | | 3 | 1 | 1 | 1 | 1 | | 4 | 1 | 1 | 2 | 1 | | 5 | 1 | 1 | 2 | 2 | | 6 | 1 | 2 | 1 | 1 | | 7 | 1 | 2 | 1 | 1 | | 8 | 1 | 3 | 2 | 2 | | 9 | 1 | 3 | 2 | 2 | 现在我需要按 col1 和 col2 对表进行分组,然后对每个分组进行统计,统计 col3=1,col4 不重复值的数量 ps:count(distinct(col4)), 统计 col3=2 , col4 值的数量 ps:count(col4)
即结果为
|id |col1|col2|distinct(col4)|count(col4)| |---|----|----|--------------|-----------| | 1 | 1 | 1 | 2 | 2 | | 2 | 1 | 2 | 1 | 0 | | 3 | 1 | 3 | 0 | 2 | 想了很久都没搞出来=。=只能厚着脸皮来这里请教了。
1 yangqi 2017-02-13 22:19:57 +08:00 你这什么意思,逻辑有问题吧?第一行 count(col4)难道不是 5 么, distinct(col4)实际上是 count(distinct(col4))? |
4 sorcerer OP @yangqi 第三列是统计 col3=1,col4 不重复值的数量 ps:count(distinct(col4)) 第四列是统计 col3=2 , col4 值的数量 ps:count(col4) |
6 sorcerer OP @yangqi 不好意思,昨晚睡了没看到,用子查询应该怎么写呢,我需要对 col1 和 col2 进行分组统计,所以 group by 肯定要的 |
7 ebony0319 2017-02-14 09:02:53 +08:00 分组后就不存在什么 ID 了把,就是一个集合的概念了。还有就是 where 过滤不是应该在之前么。 |
8 sorcerer OP @ebony0319 只要按那种格式显示出来就可以了, id 那一列去掉吧...... /td> |
10 staticor 2017-02-14 10:03:32 +08:00 试试这样的逻辑 count(distinct if(col3=2, col4, null)) |
11 sorcerer OP 谢谢 这正是我想要的,不过怎样才能 if 中 distinct 两列呢,我尝试了一下 count(distinct if(col3=2,(col4,col5),null)) 好像不行 |
13 ebony0319 2017-02-14 13:19:17 +08:00 你的 col1 , col2 聚合的时候 col3 , col4 怎么办?我想看一下原需求是什么. |
14 sorcerer OP @ebony0319 比如说 col3 代表一个账号的状态, col4 姓名, col5 是电话号码,现在我聚合后,想要统计 col3 等于 1 ,即激活状态下 col4 和 col5 两列不重复的账号数量,以及 col3 等于 2 ,即非激活状态下,账号的数量(这个不需要去重) |
15 CRVV 2017-02-14 15:06:41 +08:00 按照 staticor 的提示 SELECT col1, col2, count(DISTINCT if(col3 = 1, col4, NULL)), count(if(col3 = 2, col4, NULL)) FROM table GROUP BY col1, col2 ORDER BY col2; 就得到了原文里的结果 |
16 sorcerer OP @CRVV 嗯 这样是可以,但是 distinct 两列就不行了,试过 concat 函数 貌似不行 count(distinct (if(col3=1,concat(col4,col5),null)))...... |
17 ebony0319 2017-02-14 17:21:13 +08:00 我看懂了。给你结果你可能要吐血了。其实很简单。 首先选出不重复且激活状态的数据,然后聚合就好了。 SELECT col1, col2, col4, COUNT(col4) FROM (SELECT DISTINCT col1, col2, col3, col4 FROM temp WHERE col3 = 1) T GROUP BY col1 , col2 http://ww1.sinaimg.cn/large/af912ab4gy1fcq4du1bwgj20ak0ab0sr |
18 ebony0319 2017-02-14 17:27:37 +08:00 微博图床挂了?图片是那个结果,你按照我的思路来应该没错的。 ww1.sinaimg.cn/large/af912ab4gy1fcq4du1bwgj20ak0ab0sr |