0

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

   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. = 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.

请在此查询中帮助我。

4

1 回答 1

1
select 
   ref, 
   sum(weight) over (partition by ref order by dated) as weight,
   no,
   address
from
   ...
于 2013-03-21T07:42:08.140 回答