1

有一个我需要执行的非常简单的选择语句,由于某种原因,即使我以前做过这些,我也无法绕开它。我有一张如下表:

CustomerNo  Code    FA  DateDUE
8625475   SAFETY    A   05/02/2014
8625475   REVIEW    A   18/06/2014
8625475   CHECK     C   18/06/2014
8885742   REVIEW    A   23/01/2014
8885742   TEST      B   23/01/2014

更新后的问题更清楚:我需要选择 FA 代码为 A 且唯一带有 A 的代码是 REVIEW 的数据,因此在上面的 select 语句中它只会返回一个客户编号 (8885742) - 希望这是更清晰

4

1 回答 1

3

如果您想要客户的所有记录:

select * from your_table
where customerNo in
(
  select customerno 
  from your_table
  where FA = 'A'
  group by customerno
  having sum(case when code <> 'REVIEW' then 1 else 0 end) = 0
)

如果你只需要customerno

  select customerno 
  from your_table
  where FA = 'A'
  group by customerno
  having sum(case when code <> 'REVIEW' then 1 else 0 end) = 0

SQLFiddle 演示

于 2013-11-08T10:22:58.250 回答