如何创建 SQL 运算符组合,如 ALL IN、NOT ALL IN

发布于 2024-11-07 14:01:26 字数 1501 浏览 2 评论 0原文

我的 Filter 类中有以下 4 个属性。我将把以下 4 个属性解析为 StoredProcedure 并获取过滤结果。

/// <summary>
/// Gets and sets comma seperated condition ids.
/// Patients must have all these conditions in order to satisfy the filter.
/// </summary>
public string MustHaveAllConditions { get; set; }

/// <summary>
/// Gets and sets comma separated condition ids.
/// Patients must not have all of these conditions in order to satisfy the filter.
/// </summary>
public string MustNotHaveAllConditions { get; set; }

/// <summary>
/// Gets and sets comma separated condition ids.
/// Patients must have at least one of these conditions in order to satisfy the filter.
/// </summary>
public string MustHaveAtLeastOneCondition { get; set; }

/// <summary>
/// Gets and sets comma separated condition ids.
/// Patients must not have at least one of these conditions in order to satisfy the filter.
/// </summary>
public string MustNotHaveAtLeastOneCondition { get; set; }

我的存储过程将有四个参数,如下所示。 例如:

@*MustHaveAll*条件 = "1,2"

@*MustNotHaveAll*条件 = "3,4"

@*MustHaveAtLeastOne*条件= "5,6,7,8"

@*MustNotHaveAtLeastOne*Condition = "9, 10"

我正在使用 UDF返回一个包含 Ids 列的表。

我的问题:

基本上我可以使用 SQL“IN”运算符来查找至少具有一种条件(即: @MustHaveAtLeastOneCondition )和“NOT IN”运算符组合来过滤 @MustNotHaveAnyConditions 的患者。

是否有任何 SQL 运算符(或简单的方法)来过滤 MustHaveAllConditions、MustNotHaveAllConditions 参数?

I have below 4 properties in a Filter class. I am going to parse below 4 properties to a StoredProcedure and get the filtered result.

/// <summary>
/// Gets and sets comma seperated condition ids.
/// Patients must have all these conditions in order to satisfy the filter.
/// </summary>
public string MustHaveAllConditions { get; set; }

/// <summary>
/// Gets and sets comma separated condition ids.
/// Patients must not have all of these conditions in order to satisfy the filter.
/// </summary>
public string MustNotHaveAllConditions { get; set; }

/// <summary>
/// Gets and sets comma separated condition ids.
/// Patients must have at least one of these conditions in order to satisfy the filter.
/// </summary>
public string MustHaveAtLeastOneCondition { get; set; }

/// <summary>
/// Gets and sets comma separated condition ids.
/// Patients must not have at least one of these conditions in order to satisfy the filter.
/// </summary>
public string MustNotHaveAtLeastOneCondition { get; set; }

My Stored procedure will have four parameters like below.
Eg:

@*MustHaveAll*Conditions = "1,2"

@*MustNotHaveAll*Conditions = "3,4"

@*MustHaveAtLeastOne*Condition = "5,6,7,8"

@*MustNotHaveAtLeastOne*Condition = "9, 10"

I am using a UDF that returns a table with Ids column.

My question:

Basically I can use SQL "IN" operator to find the patients who has at least one Condition (ie : @MustHaveAtLeastOneCondition ) and "NOT IN" operator combination to filter @MustNotHaveAnyConditions.

Are there any SQL Operators(or esay ways) to filter MustHaveAllConditions, MustNotHaveAllConditions parameters ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2024-11-14 14:01:26
-- Patients
declare @Patient table (PatientID int)

-- Conditions per patient
declare @PatientCondition table (PatientID int, ConditionID int)

-- Conditions table generated from param string
declare @Condition table (ConditionID int)

-- Test data
insert into @Patient
select 1 union all
select 2 union all
select 3

insert into @PatientCondition
select 1, 1 union all
select 1, 2 union all
select 1, 3 union all
select 2, 1 union all
select 3, 3

insert into @Condition
select 1 union all
select 2


-- MustHaveAll
select *
from @Patient as P
where P.PatientID in 
  (
    select PC.PatientID
    from @PatientCondition as PC
      inner join @Condition as C
        on PC.ConditionID = C.ConditionID
    group by PC.PatientID
    having count(PC.ConditionID) = (select count(ConditionID) from @Condition)
  )

--MustNotHaveAll
select *
from @Patient as P
where P.PatientID not in 
  (
    select PC.PatientID
    from @PatientCondition as PC
      inner join @Condition as C
        on PC.ConditionID = C.ConditionID
    group by PC.PatientID
    having count(PC.ConditionID) = (select count(ConditionID) from @Condition)
  )

-- MustHaveAtLeastOne
select *
from @Patient as P
where P.PatientID in
  (
    select PC.PatientID
    from @PatientCondition as PC
      left outer join @Condition as C
        on PC.ConditionID = C.ConditionID
    where C.ConditionID is not null
  )

--MustNotHaveAtLeastOne
select *
from @Patient as P
where P.PatientID not in
  (
    select PC.PatientID
    from @PatientCondition as PC
      left outer join @Condition as C
        on PC.ConditionID = C.ConditionID
    where C.ConditionID is not null
  )
-- Patients
declare @Patient table (PatientID int)

-- Conditions per patient
declare @PatientCondition table (PatientID int, ConditionID int)

-- Conditions table generated from param string
declare @Condition table (ConditionID int)

-- Test data
insert into @Patient
select 1 union all
select 2 union all
select 3

insert into @PatientCondition
select 1, 1 union all
select 1, 2 union all
select 1, 3 union all
select 2, 1 union all
select 3, 3

insert into @Condition
select 1 union all
select 2


-- MustHaveAll
select *
from @Patient as P
where P.PatientID in 
  (
    select PC.PatientID
    from @PatientCondition as PC
      inner join @Condition as C
        on PC.ConditionID = C.ConditionID
    group by PC.PatientID
    having count(PC.ConditionID) = (select count(ConditionID) from @Condition)
  )

--MustNotHaveAll
select *
from @Patient as P
where P.PatientID not in 
  (
    select PC.PatientID
    from @PatientCondition as PC
      inner join @Condition as C
        on PC.ConditionID = C.ConditionID
    group by PC.PatientID
    having count(PC.ConditionID) = (select count(ConditionID) from @Condition)
  )

-- MustHaveAtLeastOne
select *
from @Patient as P
where P.PatientID in
  (
    select PC.PatientID
    from @PatientCondition as PC
      left outer join @Condition as C
        on PC.ConditionID = C.ConditionID
    where C.ConditionID is not null
  )

--MustNotHaveAtLeastOne
select *
from @Patient as P
where P.PatientID not in
  (
    select PC.PatientID
    from @PatientCondition as PC
      left outer join @Condition as C
        on PC.ConditionID = C.ConditionID
    where C.ConditionID is not null
  )
巴黎夜雨 2024-11-14 14:01:26

确实没有一种简单的方法可以完成您所描述的操作。我要做的可能是将您的字符串拆分为一个表变量,然后使用右连接、左连接和内连接的组合来从另一个表变量(即我的输出)中删除结果。

There really isn't an easy way to do what you're describing. What I would do is probably take your string and split it into a table variable and then use a combination of right, left and inner joins to remove results from another table variable that was my output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文