【问题标题】:Need to display records which has positive and negative value需要显示有正负值的记录
【发布时间】:2018-08-08 09:17:05
【问题描述】:

我的表中有以下数据

ID     code     date        amount   health_plan
111    A123     20170101     10      BH
111    A123     20100101    -10      BH
311    A124     20170712     20      CVA
311    A124     20170712    -20      CVA
311    A124     20170712     20      CVA
311    A124    20180201      55      CVA

我创建了一个查询来显示那些具有相同 ID、代码和日期的正值和负值的记录。运行以下查询后,我期待这种类型的输出。

111    A123    20170101      10      BH
111    A123    20100101     -10      BH
311    A124    20170712      20      CVA
311    A124    20170712     -20      CVA
WITH amt_cur
        AS (SELECT DISTINCT first_name,
                            last_name,
                            birth_date,
                            gender,
                            health_plan,
                            id,
                            code,
                            date,
                            amount
            FROM   table_a
            WHERE  (id,
                    code,
                    date,
                    health_plan) IN
                      (SELECT a.id,
                              a.code,
                              a.date,
                              a.health_plan
                       FROM   table_a a
                              JOIN table_a b
                                 ON     b.health_plan = a.health_plan
                                    AND b.id = a.id
                                    AND b.date = a.date
                                    AND b.code = a.code
                                    AND b.amount <> a.amount
                                    AND (a.amount > 0 AND b.amount < 0)))
SELECT *
FROM   amt_cur
ORDER BY id, code, date;

但我得到以下输出

111    A123     20170101      10       BH
111    A123     20100101     -10       BH
311    A124     20170712      20       CVA
311    A124     20170712     -20       CVA
311   A124      20170712      20       CVA

有人可以帮助实现结果吗?

【问题讨论】:

  • 您的数据丢失health_plan,这对于了解您的查询应该如何工作很重要。
  • 当相同(id、代码、日期)组合同时存在正数和负数时,它们是始终相同(绝对值,例如 +10 和 -10)还是可以不同( +10 和 -17)?它们总是成对出现(一负一正)吗?
  • 负值和正值可能会发生变化。当两个金额的结果 = 0 时,我必须显示这些记录。例如,如果我有 +10 和 -17,那么结果是 -7,不需要显示。
  • 所以:您的样本数据有重复的行。从顶部开始的第 5 行与第 3 行相同(所有列中的值相同)。您是说除了您所说的之外,您的要求是删除重复项?否则,不清楚为什么您通过现有查询获得的输出是“不正确的”——对我来说它看起来非常好。如果从顶部开始的第 5 行中的值不是 20,而是 40,该怎么办?那么,您是否希望输出中从 #3 到 #5 的所有三行?如果不是,还有什么附加规则?

标签: sql oracle


【解决方案1】:

简单地使用exists怎么样:

select t.*
from t
where exists (select 1 from t t2 where t2.id = t.id and t2.amount < 0) and
      exists (select 1 from t t2 where t2.id = t.id and t2.amount > 0) ;

【讨论】:

    【解决方案2】:

    此查询根据您的规则检索数据:

    with t as (
       select a.* , count(distinct a.amount)over(partition by a.id,a."DATE",a.code,a.health_plan) "count"
        , sum (a.amount) over(partition by a.id,a."DATE",a.code,a.health_plan) "sum"
       from table_a a
    )
    select a.id,a.code,a."DATE",a.amount
    from t a  where "count">1 and "sum"=0
    ;
    

    目前它什么也没产生,因为您的数据都不符合规则。
    前两行有不同的日期,接下来的三行有 sum !=0 ,最后一行没有一对。

    【讨论】:

      【解决方案3】:
      WITH amt_cur
              AS (SELECT MAX(first_name) first_name,
                                  MAX(last_name) last_name,
                                  MAX(birth_date) birth_date,
                                  MAX(gender) gender,
                                  health_plan,
                                  id,
                                  code,
                                  "date",
                                  amount
                  FROM   table_a
                  WHERE  (id,
                          code,
                          "date",
                          health_plan,
                          ABS(amount)) IN
                            (SELECT distinct a.id,
                                    a.code,
                                    a."date",
                                    a.health_plan,
                                    ABS(a.amount)
                             FROM   table_a a
                                    JOIN table_a b
                                       ON     b.health_plan = a.health_plan
                                          AND b.id = a.id
                                          AND b."date" = a."date"
                                          AND b.code = a.code
                                          AND ABS(b.amount + a.amount) < 0.00001
                                          AND (a.amount > 0 AND b.amount < 0))
        GROUP BY health_plan, id, code, "date", amount
                                          )
      SELECT *
      FROM   amt_cur
      ORDER BY id, code, "date";
      

      【讨论】:

      • 但也是比较的重要元素。负数和正数的日期应该相同。此外,逻辑类似于显示 b.amount + a.amount = 0。例如,如果给定的 ID、代码、health_plan 和日期有 10、-10 和 10 数量,那么它将只显示 10 和 -10因为结果是 0。
      • 目前缺少 b.amount + a.amount = 0 的比较。正如上面评论中提到的,您可以将 health_plan 添加到示例数据中。
      • 我在查询中尝试了逻辑 b.amount + a.amount = 0 但它不起作用。这就是我问这个问题的原因。
      • 因为数量对于查询也很重要,所以我现在已将其作为绝对值添加到 where 选择中。我添加了在我的数据库实例上运行它时有效的数量比较。
      • 金额列是typ整数还是浮点数据类型?然后你应该用“AND ABS(b.amount + a.amount) 来比较它
      猜你喜欢
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      相关资源
      最近更新 更多