sql列,其中为客户搜索特定值

时间:2017-06-25 12:33:47

标签: sql oracle

我有一个包含客户编号和其他专栏的数据库。 现在我已经准备好了这个sql,但它仍然遗漏了一件事,我无法弄清楚如何正确使用它。我需要的是让所有客户使用soort = 041并且他们也可以使用soort 040但他们不能使用soort = 043。

select  *
from tabel1 join tabel2 on 1_key_nummer = 2_key_nummer
where typen = 'regulier'
and datum_vanaf   < sysdate
and nvl(datum_tot, '31-12-9999') > sysdate
and einde_registratie is null
and soort = 041 

它尝试“不在043中”,但它仍然让我的客户回来也有043。

3 个答案:

答案 0 :(得分:0)

我想你想要这样的东西:

select customer_number
from tabel1 join
     tabel2
     on 1_key_nummer = 2_key_nummer
where typen = 'regulier' and
      datum_vanaf   < sysdate
      (datum_tot is null or datum_tot > sysdate) and
      einde_registratie is null
gropu by customer_number
having sum(case when soort in (040, 041) then 1 else 0 end) > 0 and
       sum(case when soort = 043 then 1 else 0 end) = 0;

请注意,数值前的0是可疑的。它没有任何意义。如果这是一个字符串,则使用单引号('041')。

答案 1 :(得分:0)

尝试使用它:

select  *
from tabel1 join tabel2 on 1_key_nummer = 2_key_nummer
where typen = 'regulier'
and datum_vanaf   < sysdate
and nvl(datum_tot, '31-12-9999') > sysdate
and einde_registratie is null
and ((soort = 041 OR soort = 040) and soort <> 043)

我&#39;有点混淆列soort的数据类型。它是一个INTEGER od CHAR / VARCHAR,所以请尝试:

and ((soort = '041' OR soort = '040') and soort <> '043')

答案 2 :(得分:0)

仅使用一组括号

尝试此操作
select  *
from tabel1 join tabel2 on 1_key_nummer = 2_key_nummer
where typen = 'regulier'
and datum_vanaf   < sysdate
and nvl(datum_tot, '31-12-9999') > sysdate
and einde_registratie is null
and (soort = 041 OR soort = 040) -- Here we are saying soort can be either 40 or 41
and soort <> 043--Here we are saying soort cannot be 43