筛选字符串文字时排除空值

xtfmy6hx  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(302)

我有下面的查询,我打算过滤/排除 WHERE 范围类型或市场概念等于字符串文字“placeholder”。我们发现,由于反馈,我们实际上也排除了那些字段所在的任何示例 NULL . 这让我有些惊讶,有什么建议可以解释吗?
查询:

SELECT DISTINCT M.MATERIAL, 
       A.MARKET_CONCEPT, 
       A.RANGE_TYPE
FROM VW_MRP_ALLOCATION_COMBINED M
JOIN VW_ARTICLE_ATTRIBUTES_COMBINED A ON M.Material = A.Article AND M.SALES_ORGANIZATION = A.SALES_ORGANIZATION
WHERE M.stock_type = '' 
AND A.market_concept <> 'PLACEHOLDER'
AND A.RANGE_TYPE <> 'PLACEHOLDER' 
AND A.Article in ('BK0348',
'BQ2718',
'BQ2719',
'BS3674',
'CF3607',
'CF3608',
'CF3609',
'CF3610',
'CV5091',
'D94751',
'DH6911',
'DT5039')
AND M.Sales_Organization = 6040;
eaf3rand

eaf3rand1#

空记录是使用“isnull”关键字绘制的,用于比较。下面是一个如何获取空记录的示例

with data
  as (select 'PLACEHOLDER' as market_concept,'PLACEHOLDER' as range_type
      union all
      select 'MarketConcept1' as market_concept,'Rangetype1' as range_type
      union all
      select null as market_concept, null as range_type
      )
 select *
 from data
 where ((market_concept <> 'PLACEHOLDER'
         and range_type <>'PLACEHOLDER'
         )
      --This OR condition brings out the records which are null
      OR(market_concept is null
         and range_type is null
         )
       )

+----------------+------------+
| market_concept | range_type |
+----------------+------------+
| MarketConcept1 | Rangetype1 |
| null           | null       |
+----------------+------------+
li9yvcax

li9yvcax2#

我建议说得明确点:

(A.market_concept <> 'PLACEHOLDER' OR A.market_concept IS NULL) AND
(A.RANGE_TYPE <> 'PLACEHOLDER' A.range_type IS NULL) AND

注:假设 'PLACEHOLDER' 不是 NULL . 如果可能的话,我建议问一个新问题,有清晰的样本数据和期望的结果。

tcomlyy6

tcomlyy63#

可以使用isnull函数将null值视为与占位符相同。

SELECT DISTINCT M.MATERIAL, 
       A.MARKET_CONCEPT, 
       A.RANGE_TYPE
FROM VW_MRP_ALLOCATION_COMBINED M
JOIN VW_ARTICLE_ATTRIBUTES_COMBINED A ON M.Material = A.Article AND M.SALES_ORGANIZATION = A.SALES_ORGANIZATION
WHERE M.stock_type = '' 
AND ISNULL(A.market_concept,'PLACEHOLDER') <> 'PLACEHOLDER'
AND ISNULL(A.RANGE_TYPE,'PLACEHOLDER') <> 'PLACEHOLDER' 
AND A.Article in ('BK0348',
'BQ2718',
'BQ2719',
'BS3674',
'CF3607',
'CF3608',
'CF3609',
'CF3610',
'CV5091',
'D94751',
'DH6911',
'DT5039')
AND M.Sales_Organization = 6040;

相关问题