我有一张表,其中前两行是Company,Year。每家公司都有一些年份,但不一定都是这些年份:
ABC | 2010
ABC | 2011
ABC | 2012
BBC | 2011 //does not have all the years, don't want to select it
我想选择一年中的公司列表(不仅仅是其中一些公司),但我在编写选择查询时遇到了麻烦。我想这很容易,但由于某些原因我无法弄明白。
答案 0 :(得分:3)
试
select company
from your_table
group by company
having count(distinct year) = (select count(distinct year) from your_table)
答案 1 :(得分:3)
Select * FROM Company Where CompanyId In(
select CompanyId From Company
group by CompanyId
having count(*) = (select count(distinct Year) from Company)
)
http://www.sqlfiddle.com/#!3/c2f81/11
请注意,如果您已经知道应该有多少年,那么显然您只需说出这个数字,而不是选择不同年份。
答案 2 :(得分:0)
SELECT Company
FROM Table
GROUP BY Company
HAVING COUNT(Distinct YEAR) = 3