mysql 两列分组的列数总和
INSERT INTO tablex(Id, Name, Team, Joined) VALUES
(1, 'Mr. A', 'X', '2011-02-22'),
(2, 'Mr. B', 'Y', '2011-02-11'),
(3, 'Ms. C', 'X', '2011-02-10'),
(4, 'Mr. D', 'Y', '2011-01-12'),
(5, 'Ms. E', 'X', '2011-01-06'),
(6, 'Mr. F', 'Y', '2011-05-02');
(7, 'Mr. H', 'X', '2011-02-01');
所需的输出是:
Month | Team | TotalMembers | Percentage
01/2011 | X | 1 | 50
01/2011 | Y | 1 | 50
02/2011 | X | 2 | 75
02/2011 | Y | 1 | 25
05/2011 | X | 0 | 0
05/2011 | Y | 1 | 100
请帮我完成上述操作。在 MySQL 中最受青睐(GROUP BY Team, DATE_FORMAT(Joined, '%m/%Y'))。但可以使用PHP。
提前致谢
INSERT INTO tablex(Id, Name, Team, Joined) VALUES
(1, 'Mr. A', 'X', '2011-02-22'),
(2, 'Mr. B', 'Y', '2011-02-11'),
(3, 'Ms. C', 'X', '2011-02-10'),
(4, 'Mr. D', 'Y', '2011-01-12'),
(5, 'Ms. E', 'X', '2011-01-06'),
(6, 'Mr. F', 'Y', '2011-05-02');
(7, 'Mr. H', 'X', '2011-02-01');
output required is:
Month | Team | TotalMembers | Percentage
01/2011 | X | 1 | 50
01/2011 | Y | 1 | 50
02/2011 | X | 2 | 75
02/2011 | Y | 1 | 25
05/2011 | X | 0 | 0
05/2011 | Y | 1 | 100
Please help me to do the above. Mostly preferred in MySQL (GROUP BY Team, DATE_FORMAT(Joined, '%m/%Y')). but can use PHP.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以有更优雅的解决方案,但这个应该可行:
There can be more elegant solutions but this one should work: