在oracle中选择查询

时间:2013-03-21 07:32:59

标签: oracle

我有像这样的数据库设计......

   dated       ref       weight  no.  address

21-03-2013  ABCD/EDFG     1234   A45   A1 
20-03-2013  ABCD/EDFG     789    A56   A2
25-03-2013  ABCD/EDFG     6981   A99   A5 
23-03-2013  GAJHS/ASDH     72    A82   GV

我在查询结果中想要的是这样的......  在 no。的基础上搜索  但是,它必须看看该行的 ref 是否存在更多次,如果它存在,那么它必须添加所有这些行的权重,记住日期所有此类记录应小于所选否。的日期。

示例 -

no. = A56
three rows exist with same ref(ABCD/EDFG)
but dated of A56 is lower among all so results should be 

     ref ------ weight --------  no. -------- address
ABCD/EDFG ------ 789 ----------- A56 -------- A2

但是在没有的情况下。 = A99结果应该是这样的 -

ref ----------- weight --------  no. -------- address
ABCD/EDFG --- (789+6981+1234) ----------- A99 -------- A5 

as dated of A99 is greater than other two records.

请在此查询中帮助我。

1 个答案:

答案 0 :(得分:1)

select 
   ref, 
   sum(weight) over (partition by ref order by dated) as weight,
   no,
   address
from
   ...