满足特定要求的支票账户

Checking accounts satisfying specific requirements

我需要确定在特定日期(2010 年 2 月)评级的帐户,而不是在另一个日期(2010 年 12 月)评级的帐户。

table长得像

account date               Rated
451234  January 2008       rated
451234  February 2008      rated
451234  March 2008         not rated
451234  December 2010      not rated
214211  January 2008       rated
214211  February 2009      rated
241243  March 2011         rated
241243  December 2010      not rated
241243  March 2009         not rated
241243  April 2009         not rated
241243  March 2012         not rated
241243  December 2011      not rated

上面显示的 table 是由内部联接创建的:

select * from 
tab1 as t1
inner join tab2 as t2
on t1.account=t2.account
and t1.date=t2.date

您知道如何查看在 2010 年 2 月评级但在 2010 年 12 月不再评级的帐户吗?

我期望的结果:

account date               Rated
451234  February 2008      rated
451234  December 2010      not rated
241243  December 2010      not rated

I need to identify accounts that were rated on a specific date (February 2010) and no more in another one (December 2010).

如果我没理解错的话,一种方法是聚合 having:

select account
from tab1
group by account
having sum(case when date = 'February 2010' and rated = 'rated' then 1 else 0 end) > 0 and
       sum(case when date = 'Decemner 2010' and rated = 'not rated' then 1 else 0 end) > 0 ;

替代方法是 not exists:

select t1.*
from tab1 t1
where t1.date = 'February 2010' and
      t1.rated = 'rated' and
      not exists (select 1
                  from tab1 tt1
                  where tt1.account = t1.account and
                        tt1.date = 'December 2010' and
                        tt1.rated = 'not rated'
                 );