我有一个我想查询的Sqlite数据库。我的基本SQL知识使我能够查询我需要的大部分内容但是我被困在这里:
该表看起来像这样(简化!):
| Name | Year | Title | Musician 1 | Musician 2 |
| --------------- | ---- | ------- | ---------- | ----------- |
| Doe, Jon | 2007 | Title 1 | Beatles | The Stooges |
| May, Peter | 2001 | Title 2 | | Beatles |
| Schmidt, Andrea | 1997 | Title 3 | Nick Cave | |
我希望能够查询Top Musician in the 2000s
并获得类似
| Name | Count |
| ----------- | ----- |
| Beatles | 2 |
| The Stooges | 1 |
你怎么会这样做?谢谢!
答案 0 :(得分:3)
如果我理解正确,你需要这个
select count(*), name from (
select Musician_1 as Name from table where Year >= 2000
union all
select Musician_2 as Name from table where Year >= 2000
) t
group by Name
order by count(*) desc
答案 1 :(得分:2)
你可以使用联盟全部
select musician, count(*) from (
select year, musician_1 as musician
from my_table
union all
select year, musician_2
from my_table
) t
where t.year between 2000 and 2010
group by t.musician