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

问题描述 投票:0回答:3

我的表中有以下数据

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

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

sql oracle
3个回答
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) ;

0
投票

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

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,而last没有一对。


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";
© www.soinside.com 2019 - 2024. All rights reserved.