我有一张桌子(“名字”)有三列(“第一”,“最后”和“俱乐部”)。有几个重复名字的名字和几个重复自己的姓氏,还有4个俱乐部。有几个学生在不止一个俱乐部。一小部分数据样本低于......
表:名称
------------------------------------
| last |first |club |
------------------------------------
|Doe |John |Fencing Club |
|Doe |Jane | Swim Club |
|Doe | Jane |Tennis Club |
|Frank |Kurt |Computer Club |
|Frank |Kurt |Swim Club |
|Frank |Kurt |Fencing Club |
|Frank |Melissa |Computer Club |
|Frank |Melissa |Tennis Club |
|Jackson |John |Fencing Club |
|Peters |Ashley |Computer Club |
|Peters |Ashley |Fencing Club |
|Peters |Ashley |Swim Club |
|Peters |Ashley |Tennis Club |
------------------------------------
我希望这样做是为了创建一份简明的报告,我会根据他们所在的俱乐部数量合并信息,只显示学生人数。
输出应该看起来像这样......
-----------------------------
|Students| Clubs Per Student|
|2 | 1 |
|2 | 2 |
|1 | 3 |
|1 | 4 |
-----------------------------
关于如何构建这个的任何想法?我一直在捣乱我的大脑并研究了几个小时,但我似乎无法想出一个可行的解决方案。谢谢!
答案 0 :(得分:0)
Select concat(last, ', ', first) as 'Student', count(club) from name group by concat(last, ', ', first)
但是如果有多个同名学生,你应该给他们所有的id#s。在这种情况下,您希望按学生ID#。
进行分组编辑:
我误解了你在找什么。你会想要:
Select count(Student) as 'Students', clubs from (Select concat(last, ', ', first) as 'Student', count(club) as 'clubs' from name group by concat(last, ', ', first)) a group by clubs