我有一张这样的表:
我想编写一个查询,仅输出2000年和2001年两次出现的记录。
在这种情况下,输出将是
Bb 2000
Bb 2001
因为只有名称为Bb的记录在两年都有出现。
我尝试过Subqueries和Joins但没有成功,因为这两列不是主键,也不是外键。
谢谢。
答案 0 :(得分:1)
您可以使用intersect
执行此操作。
select x.name, x.year from
(
select name from table where year = 2000
intersect
select name from table where year = 2001
) t join table x on t.name = x.name
答案 1 :(得分:1)
我的表格只能在
2001
列中有year
或year
。没有其他年份会出现。但是,如果name
可能有其他值,那么我希望获得同一select id, name, year from (select id, name, year, count(distinct year) over (partition by name) as distinct_year_cnt from table_name) where distinct_year_cnt > 1
一年以上的记录。
如果您有需要处理评论中描述的其他年份,那么这就是我要使用的查询:
{{1}}