找到两行之间的差异

时间:2018-12-05 19:35:52

标签: sql oracle

我需要找到下表中c2行之间的差异

SEQ_ID  Priv_ID Common_ID Source_ID C1  C2
------ -------- --------- --------- -- ---
1          1    C001        S1  abc 32331299300
2          1    C001        S1  def 12656678121
3          1    C001        S1  ghi 8966492700
4          1    C001        S2  abc 32331292233
5          1    C001        S2  ghi 8966492700
6          1    C001        S2  def 12656672000

预期输出应如下所示,

SEQ_ID  Priv_ID Common_ID   C1  C2
------ -------- ---------   -- ---
1          1    C001        abc 7067
2          1    C001        def 6121
3          1    C001        ghi 0

请协助。

2 个答案:

答案 0 :(得分:1)

这个怎么样?我没有使用所有行都相同的列(因此它们没有任何区别)。

SQL> with test (seq_id, source_id, c1, c2) as
  2    (select 1, 's1', 'abc', 32331299300 from dual union all
  3     select 2, 's1', 'def', 12656678121 from dual union all
  4     select 3, 's1', 'ghi', 8966492700  from dual union all
  5     select 4, 's2', 'abc', 32331292233 from dual union all
  6     select 5, 's2', 'ghi', 8966492700  from dual union all
  7     select 6, 's2', 'def', 12656672000 from dual
  8    )
  9  select min(seq_id) seq_id,
 10    c1,
 11    max(case when source_id = 's1' then  c2 end) +
 12    max(case when source_id = 's2' then -c2 end) c2
 13  from test
 14  group by c1
 15  order by 1;

    SEQ_ID C1          C2
---------- --- ----------
         1 abc       7067
         2 def       6121
         3 ghi          0

SQL>

答案 1 :(得分:0)

嗯。 。 。一种方法是条件聚合。但是关键是v8::Local<v8::Object> holder = args.Holder(); v8::Local<v8::Object> This = args.This(); v8::Local<v8::Value> proto = holder->GetPrototype(); if (proto->IsObject()) { This->SetPrototype(args.GetIsolate()->GetCurrentContext(), proto); }

row_number()