Ниже приведен большой запрос, он работает для 4 столбцов (4 виртуальных стола), но не для 12 столбцов, что было бы лучшим способом сделать это.группа mysql по одному столбцу, но агрегируется на нескольких столбцах?
Ниже запрос для 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);
Я попытался это выберите user_id, SUM (rank = 1) как count1, SUM (rank = 1 и is_fresh = 1) как fresh1, SUM (rank = 1 и is_new = 1) как new1, AVG (возраст) как age1 от пользователей gro вверх пользователем_id; но его предоставление другого результата – vishal
AVG (возраст) не эквивалентен ни одному из ваших вариантов, используйте SUM (IF (ранг = 1, возраст, 0))/SUM (ранг = 1) AS rank_1_avg_age для вычисления среднего возраста на группу, замените rank = 1 другими условиями по мере необходимости – Naktibalda
, даже это также не работает, выберите user_id, SUM (rank = 1) как count1, SUM (rank = 1 и is_fresh = 1) как fresh1, SUM (rank = 1 и is_new = 1) как new1 из группы пользователей пользователем_id; – vishal