[英]mysql group by single column but aggregate on multiple columns?
下面是一个大查询,它适用于4列(4个虚拟表),但不适用于12列,这是更好的方法。
下面是查询4列,
select tbl1.count1, tbl2.fresh1, tbl3.new1, tbl4.score1, tbl1.user_id from
(
(select count(id) as count1, user_id
from user_ranking
where rank = 1
group by user_id
) as tbl1
JOIN
(select count(id) as fresh1, user_id
from user_ranking
where rank = 1 and is_fresh = 1
group by user_id
) as tbl2 ON tbl1.user_id = tbl2.user_id
JOIN
(select count(id) as new1, user_id
from user_ranking
where rank = 1 and is_new = 1
group by user_id
) as tbl3 ON tbl2.user_id = tbl3.user_id
JOIN
(select AVG(score) as score1, user_id
from user_ranking
where rank = 1 group by user_id
) as tbl4 ON tbl3.user_id = tbl4.user_id
);
以上对我来说很好,但不适用于12列。
如下面的答案所示,我尝试了
select user_id, SUM(rank = 1 ) as count1, SUM(rank = 1 and is_fresh = 1) as fresh1, SUM(rank = 1 and is_new = 1 ) as new1,
SUM(IF(rank=1, score, 0))/SUM(rank=1) as score1 from user_ranking group by user_id;
它给出了与以上4列查询不同的结果。
下面是查询不起作用的12列
select tbl1.user_id,
tbl1.count1, tbl2.fresh1, tbl3.new1, tbl4.score1,
tbl5.count2, tbl6.fresh2, tbl7.new2, tbl8.score2,
tbl9.count3, tbl10.fresh3, tbl11.new3, tbl12.score3
from
(
(select count(id) as count1, user_id
from user_ranking
where rank = 1
group by user_id
) as tbl1
JOIN
(select count(id) as fresh1, user_id
from user_ranking
where rank = 1 and is_fresh = 1
group by user_id
) as tbl2
JOIN
(select count(id) as new1, user_id
from user_ranking
where rank = 1 and is_new = 1
group by user_id
) as tbl3
JOIN
(select AVG(score) as score1, user_id
from user_ranking
where rank = 1 group by user_id
) as tbl4
JOIN
(select count(id) as count2, user_id
from user_ranking
where rank = 2
group by user_id
) as tbl5
JOIN
(select count(id) as fresh2, user_id
from user_ranking
where rank = 2 and is_fresh = 1
group by user_id
) as tbl6
JOIN
(select count(id) as new2, user_id
from user_ranking
where rank = 2 and is_new = 1
group by user_id
) as tbl7
JOIN
(select AVG(score) as score2, user_id
from user_ranking
where rank = 2 group by user_id
) as tbl8
JOIN
(select count(id) as count3, user_id
from user_ranking
where rank = 3
group by user_id
) as tbl9
JOIN
(select count(id) as fresh3, user_id
from user_ranking
where rank = 3 and is_fresh = 1
group by user_id
) as tbl10
JOIN
(select count(id) as new3, user_id
from user_ranking
where rank = 4 and is_new = 1
group by user_id
) as tbl11
JOIN
(select AVG(score) as score3, user_id
from user_ranking
where rank = 3 group by user_id
) as tbl12
);
表架构
CREATE TABLE IF NOT EXISTS `user_ranking` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`rank` int(11) NOT NULL,
`is_fresh` int(11) NOT NULL,
`is_new` int(11) NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
--
-- Dumping data for table `user_ranking`
--
INSERT INTO `user_ranking` (`id`, `user_id`, `rank`, `is_fresh`, `is_new`, `score`) VALUES
(1, 1, 1, 1, 0, 25),
(2, 2, 1, 1, 0, 67),
(3, 1, 2, 0, 1, 34),
(4, 2, 2, 0, 1, 24),
(5, 2, 1, 0, 1, 36),
(6, 1, 2, 1, 1, 1),
(7, 1, 1, 0, 1, 75);
只需将条件从每个WHERE移至SUM
SELECT
user_id,
SUM(rank = 1) AS rank1,
SUM(rank = 2) AS rank2,
SUM(rank = 2 and is_fresh = 1 ) AS rank2_fresh,
SUM(IF(rank = 1, age, 0))/SUM(rank = 1) AS rank_1_avg_age
...
FROM users
GROUP BY user_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.