SQL-如果一条记录具有特定值,则仅获取该记录,否则获取全部

时间:2019-06-06 17:12:16

标签: sql oracle oracle10g oracle-sqldeveloper

我有这个查询,它返回多个记录,如果一列中的值之一等于一个数字,我只想拥有该特定记录,否则,我想要拥有所有记录。

我尝试使用一些子查询来实现它,但是在SQL方面我不是那么好。

class C:           
    def __str__(self):
        return str(f"{self.__class__.__name__} class str ")

C.__repr__=C.__str__       
ci = C()    


print(ci)       #C class str 
print(str(ci))  #C class str 
print(repr(ci)) #C class str 

对于这个Material_Number,我有这个输出

Select       
    Material_Number,
    Supplier_Number,
    CODE
from 
    w_supp_ds
    where 
     Material_Number = '111111'

很好!

但是如果我得到这样的输出:

111111  1015221     blank
111111  1071384     blank

我只想要第一条记录。因此,如果一条记录的“代码”列中包含数字,则得到该记录,否则,我得到两个空白。这样我就不能使用WHERE IsNumeric(Code)

4 个答案:

答案 0 :(得分:1)

一种方法是not exists条件:

select . . .
from w_supp_ds w
where Material_Number = '111111' or
      not exists (select 1 from w_supp_ds w2 where w2.Material_Number = '111111')

答案 1 :(得分:0)

如果我对您的理解正确,那么RANK分析功能可能会有所帮助:

SQL> with w_supp_ds (material_number, supplier_number, code) as
  2    (select 111111, 1015221, null from dual union all
  3     select 111111, 1071384, null from dual union all
  4     --
  5     select 222222, 1074556, 2    from dual union all
  6     select 222222, 1001297, null from dual
  7    ),
  8  temp as
  9    (select material_number, supplier_number, code,
 10        rank() over (order by code desc nulls last) rn
 11     from w_supp_ds w
 12     where Material_Number = &par_mat_num
 13    )
 14  select material_number, supplier_number, code
 15  from temp
 16  where rn = 1;
Enter value for par_mat_num: 111111

MATERIAL_NUMBER SUPPLIER_NUMBER       CODE
--------------- --------------- ----------
         111111         1015221
         111111         1071384

SQL> /
Enter value for par_mat_num: 222222

MATERIAL_NUMBER SUPPLIER_NUMBER       CODE
--------------- --------------- ----------
         222222         1074556          2

SQL>

答案 2 :(得分:0)

在WHERE子句中带有CASE语句:

Select       
  Material_Number,
  Supplier_Number,
  CODE
from w_supp_ds
where 
  Material_Number = '??????'
  and coalesce(CODE, '0') = case 
    when exists (select 1 from w_supp_ds where Material_Number = '??????' and CODE is not null) then CODE
    else '0'
  end

请参见demo
Material_Number = '111111'的结果:

> MATERIAL_NUMBER | SUPPLIER_NUMBER | CODE
> :-------------- | :-------------- | :---
> 111111          | 1015221         | null
> 111111          | 1071384         | null

Material_Number = '222222'的结果:

> MATERIAL_NUMBER | SUPPLIER_NUMBER | CODE
> :-------------- | :-------------- | :---
> 222222          | 1074556         | 2   

答案 3 :(得分:0)

尝试一下:

SELECT
    MATERIAL_NUMBER,
    SUPPLIER_NUMBER,
    CODE
FROM
    (
        SELECT
            MATERIAL_NUMBER,
            SUPPLIER_NUMBER,
            CODE,
            COUNT(CODE) OVER(
                PARTITION BY MATERIAL_NUMBER
            ) M
        FROM
            W_SUPP_DS
        WHERE
            MATERIAL_NUMBER = '111111' --222222
    )
WHERE
    M = 0
    OR ( CODE IS NOT NULL )

Demo

干杯!