2017-07-17 80 views
0

我是SQL新手,我需要您的專業知識才能達到以下要求。請幫幫我 。查找與複雜彙總重複

我有兩個數據集:數據集1:

id Item Amount 
1 apple 100 
2 apple -100 
3 apple 200 
4 apple -200 
5 apple 100 

輸出應該

數據組2:

id Item Amount 
1 apple 100 
2 apple -100 
3 apple 200 
4 apple -200 
5 apple 100 
6 apple 100 

輸出應該

id Item Amount 
6 apple 100 

功能:爲我付出的項目「蘋果」 100(1),由於一些條件我已經回到了蘋果店老闆讓我找回我的錢-100(2),

再次我買蘋果所以金額將是200(3)(金額增加),我再次如此-200(4)返回。

最後我付了100(5)並再次購買蘋果。但這一次系統重複我的最終付款100(5)100(6)。

我需要找到系統是否複製我的交易或不併顯示重複的交易

+1

首先,你需要一個可靠的標準dubles。如果你真的買2'蘋果100'呢? – Serg

+0

修復正在複製的系統,記錄不應該保存在首位。 –

+0

@Serg:我買一個蘋果。成本將根據時間推遲。 – user7334955

回答

0

你似乎想最後一排時的正金額數量超過1超出的負數金額數:

select t.* 
from (select t.* 
     from t 
     order by id desc 
    ) t 
where rownum = 1 and 
     (select sum(case when amount > 0 then 1 
         when amount < 0 then -1 
        end) 
     from t 
    ) >= 2