【问题标题】:Is is possible to run a query with multiple lines where one line meets one criteria and the others meet different criteria?是否可以运行具有多行的查询,其中一行满足一个条件而其他行满足不同的条件?
【发布时间】:2021-03-22 06:31:31
【问题描述】:

我正在尝试识别符合以下标准的声明:

  1. 每一行都必须有拒绝代码“X”
  2. 一行的允许金额必须大于 0.00 美元
  3. 其余行的允许金额必须等于 $0.00

示例:声明 ID 123456 有三行与此 ID 关联。第 1 行允许 40.00 美元。第 2 行和第 3 行允许 0.00 美元。使用此查询,Claim ID 123456 将被拉取。

我能想到的唯一方法是对所有具有拒绝代码“X”的声明运行查询,然后在 Excel 中手动搜索。

基本上我的查询是:

SELECT DISTINCT
Claim ID,
Line Number,
Denial Code,
Line Allowed Amount
(FROM)
WHERE 
Denial Code = 'X'

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。

标签: sql aggregate-functions having-clause dbvisualizer


【解决方案1】:

这听起来像是使用having 子句进行聚合和过滤:

select claimid
from mytable
group by claimid
having 
    min(case when denial = 'X' then 1 else 0 end) = 1   -- all rows have "denial" = "X"
    and max(lineallowedamount) > 0                      -- one row has an amount greater than 0
    and max(lineallowedamount) = sum(lineallowedamount) -- all other rows have amount 0

【讨论】:

    【解决方案2】:

    我会将我们所能做的转移到where 子句并使用having 子句来确保我们只包括那些只有一行的数量> 0而其他所有为0的claimIds,这只有在@987654323时才有可能@ 是真的。

    select claimId
    from your_table
    where denial = 'X' and lineallowedamount > 0 
    group by claimId
    having max(lineallowedamount) = sum(lineallowedamount);
    

    如果您想从表中获取针对这些声明 ID 过滤的数据,您可以使用如下子查询

    select *
    from your_table
    where claimId in (select claimId
                      from your_table
                      where denial = 'X' and lineallowedamount > 0 
                      group by claimId
                      having max(lineallowedamount) = sum(lineallowedamount));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多