如果组1的值大于组2的值,则找出字段组之间的差异

时间:2019-04-15 15:46:45

标签: sql oracle oracle11g difference

我有一个表格“ Components”,其中包含以下字段

Person ID    Date_from      CompType       Value
000001       01/01/2003       A1            100
000001       01/01/2003       B1            200
000001       01/01/2003       C1            150
000001       01/01/2003       D1            180
000001       01/01/2003       E1            185
000001       01/01/2002       A1            125
000001       01/01/2002       B1            020
000001       01/01/2002       C1            130
000001       01/01/2002       D1            160
000001       01/01/2002       E1            105
000001       01/01/2001       A1            090
000001       01/01/2001       B1            200
000001       01/01/2001       C1            250
000001       01/01/2001       D1            160
000001       01/01/2001       E1            185

我需要找到最大DATE_FROM(01/01/2003)行中作为S1的(A1 + B1 + C1 + D1)的总和与作为S2的(A1 + B1 + C1 + D1)的总和之间的差下一个日期S1-S2 <> 0

类似地,我需要找到前一行中作为S2的(A1 + B1 + C1 + D1)的总和与下一个日期的S3的(A1 + B1 + C1 + D1)的总和之间的差,其中,S3- S2 <> 0

所以,我的输出将是

ID           Date        Current Difference   Previous Difference
000001       01/01/2003          195             245

此外,如果我在2003年1月1日和2002年1月1日的数据中没有发现任何差异。 SQL应该研究01/01/2003和01/01/2001数据集的区别。

2 个答案:

答案 0 :(得分:0)

您的问题有点难以理解。我认为以下内容基本上可以获取所需的数据-但这将返回三行而不是1。

def delta_perc(x, y):
    if x == 0:
        return 0
    else:
        return (x - y) / x * 100

# Because remember `apply` takes a function that gets a row (or column) passed to it
ordini["discount"] = ordini.apply(
    lambda row: delta_perc(row['revenue1'], row['revenue2']),
    axis=1
)

在Oracle 12C中,您可以添加select personId, date_from, sum(case when comptype in ('A1', 'B1', 'C1', 'D1') then value end) as current_sum, lag(sum(case when comptype in ('A1', 'B1', 'C1', 'D1') then value end)) over (partition by personId order by min(date_from) as prev_sum, lag(sum(case when comptype in ('A1', 'B1', 'C1', 'D1') then value end), 2) over (partition by personId order by min(date_from) as prev_sum, from t group by personId, date_from order by personId, date_from desc; 以获得第一行。

答案 1 :(得分:0)

您可以将lag()函数用于聚合,并在主查询中使用abs()作为

with Components(Person_ID, Date_from, CompType, Value) as
(
 select '000001',date'2003-01-01','A1',100 from dual union all
 select '000001',date'2003-01-01','B1',200 from dual union all
 select '000001',date'2003-01-01','C1',150 from dual union all
 select '000001',date'2003-01-01','D1',180 from dual union all
 select '000001',date'2003-01-01','E1',185 from dual union all
 select '000001',date'2002-01-01','A1',125 from dual union all
 select '000001',date'2002-01-01','B1',20  from dual union all
 select '000001',date'2002-01-01','C1',130 from dual union all
 select '000001',date'2002-01-01','D1',160 from dual union all
 select '000001',date'2002-01-01','E1',105 from dual union all
 select '000001',date'2001-01-01','A1',90  from dual union all
 select '000001',date'2001-01-01','B1',200 from dual union all
 select '000001',date'2001-01-01','C1',250 from dual union all
 select '000001',date'2001-01-01','D1',160 from dual union all
 select '000001',date'2001-01-01','E1',185 from dual
), t2 as
(
select Person_ID, Date_from, sum(Value) as sum1, 
       lag(sum(Value),1,0) over (order by Date_from) as sum2,
       lag(sum(Value),2,0) over (order by Date_from) as sum3
  from Components
 where CompType != 'E1' 
 group by Person_ID, date_from
)
select Person_ID, date_from, 
       abs(sum1-sum2) as "Current Difference",
       abs(sum2-sum3) as "Previous Difference" 
  from t2
 where sum1 * sum2 * sum3 > 0;

PERSON_ID   DATE_FROM   Current Difference  Previous Difference
000001      01.01.2003        195                  265

Demo