2

我有一个表,其中前两行是公司,年份。每家公司都会有几年,但不一定是全部:

ABC | 2010
ABC | 2011
ABC | 2012
BBC | 2011 //does not have all the years, don't want to select it

我想选择一个拥有所有年份的公司列表(不仅仅是其中一些),但我在编写一个选择查询来做到这一点时遇到了麻烦。我想这真的很容易,但由于某种原因我无法弄清楚。

4

3 回答 3

3

尝试

select company
from your_table
group by company
having count(distinct year) = (select count(distinct year) from your_table)
于 2012-10-05T16:37:07.473 回答
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

请注意,如果您已经知道应该有多少年,那么显然您只会说出那个数字而不是选择不同的年份。

于 2012-10-05T16:37:32.050 回答
0
SELECT Company
FROM Table
GROUP BY Company
HAVING COUNT(Distinct YEAR) = 3
于 2012-10-05T16:38:56.410 回答