单列的mysql组,但多列聚合?

时间:2014-01-29 12:56:55

标签: mysql join group-by

下面是大查询,它适用于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);

1 个答案:

答案 0 :(得分:3)

只需将条件从每个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