需要显示具有正值和负值的记录

时间:2018-02-28 13:14:10

标签: sql oracle

我的表格中有以下数据

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

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

3 个答案:

答案 0 :(得分:2)

如何简单地使用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) ;

答案 1 :(得分:0)

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";

答案 2 :(得分:0)

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

var selected_object = array[key=selected_id];

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