简体   繁体   English

根据多个过滤条件排除记录-SQL访问

[英]Excluding Records based on Multiple Filter Criteria- SQL Access

I have a predefined set of filter criteria that should exclude records if the expression proves true. 我有一组预定义的过滤条件,如果表达式证明是正确的,则应排除记录。 Ex: Exclude records where Trade Status = 'MAT' AND exclude records where Portfolio is IN ('Assume', 'HDLTV'). 例如:排除贸易状态为“ MAT”的记录,并排除投资组合为IN的记录(“假设”,“ HDLTV”)。 An AND function doesnt work because it would look for records where both conditions are true. AND函数不起作用,因为它会在两个条件都为真的情况下查找记录。 An OR function wont work either because if a record is held true by one and not the other, then it will return the record. OR函数不会起作用,因为如果一个记录保持为真,而另一个记录却不为真,则它将返回记录。

Is there some way to perhaps exclude records where the filter criteria is met for multiple conditions. 是否有某种方法可能排除在多个条件下都满足过滤条件的记录。 The goal is to filter the unwanted records out of the original dataset. 目的是从原始数据集中过滤掉不需要的记录。 In the query below im just trying to get the excluded population first, just to try things out. 在下面的查询中,im只是想首先获取被排除的人口,只是为了进行尝试。 Any help is much appreciated. 任何帮助深表感谢。

Trade ID    Trade Status    Trade Type  Portfolio
             VER            BondForward HD7
             VER           BondForward  HD7
             VER               EQOPT    HD7
             VER               EQOPT    HD7
 82689       VER              FUTURE    INCOMFLEX
 82104       VER              FUTURE    CAPHEDGE

SELECT HedgeFile.[Trade ID], HedgeFile.[Trade Status], HedgeFile.[Trade Type], HedgeFile.Portfolio FROM HedgeFile WHERE (HedgeFile.[Trade Status] = 'MAT') OR HedgeFile.[Portfolio] IN ('ASSUME', 'HDLTV', 'INCOMFLEX', 'CAPHEDGE') OR (HedgeFile.Hedgecode = 'WSP') OR (HedgeFile.[Trade Type] <> 'FUTURE') order by HedgeFile.[Trade Type]

I think you want not . 我想你not One way to write this is: 一种写法是:

SELECT HedgeFile.[Trade ID], HedgeFile.[Trade Status], HedgeFile.[Trade Type], 
HedgeFile.Portfolio
FROM HedgeFile
WHERE (NOT HedgeFile.[Trade Status] = 'MAT') OR
      (NOT HedgeFile.[Portfolio] IN ('ASSUME', 'HDLTV', 'INCOMFLEX', 'CAPHEDGE') OR
      (NOT HedgeFile.Hedgecode = 'WSP') OR
      (NOT HedgeFile.[Trade Type] <> 'FUTURE')
order by  HedgeFile.[Trade Type]

Of course, you can simplify this to: 当然,您可以将其简化为:

SELECT HedgeFile.[Trade ID], HedgeFile.[Trade Status], HedgeFile.[Trade Type], 
HedgeFile.Portfolio
FROM HedgeFile
WHERE (HedgeFile.[Trade Status] <> 'MAT') OR
      (HedgeFile.[Portfolio] NOT IN ('ASSUME', 'HDLTV', 'INCOMFLEX', 'CAPHEDGE') OR
      (HedgeFile.Hedgecode <> 'WSP') OR
      (HedgeFile.[Trade Type] = 'FUTURE')
order by  HedgeFile.[Trade Type];

The logic may be slightly more complicated if any of the columns can take on NULL values. 如果任何列都可以采用NULL值,则逻辑可能会稍微复杂一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM