累加功能的實現 (累計計算)

來源:互聯網
上載者:User
使用者,日 期,   當日消費金額。
001   2013-1-10  100
002   2013-1-10  200
001   2013-1-11  50
002   2013-1-11  80
001   2013-1-12  300
得到
使用者,日期,  當日消費金額,累計消費金額
001   2013-1-10  100   100
002   2013-1-10  200   200
001   2013-1-11  50    150
002   2013-1-11  80    280
001   2013-1-12  300   450實現方式如下:mysql支援:SELECT FIELD1,FIELD2,FIELD3,
       ( SELECT SUM(FIELD3)
         FROM   TEST_AA
         WHERE  FIELD1 = A.FIELD1
                AND FIELD2 <= A.FIELD2
       ) AS FIELD4
FROM   TEST_AA A但hive不支援這個。mysql實現方式二:(QQ網友西瓜小王子(365742944)  提供 ):select a.f1,a.f2,a.f3,sum(b.f3) from (select f1,f2,f3 from test_A) a join (select f3 from test_A ) b on (a.f1 = b.f1 and a.f2 >= b.f2)
group by a.f1,a.f2.a.f3 但由於hive不支援 join 條件 on中不等於,故也無法在hive中實施。還有一種方式:
select a.id,a.date,a.num as nu,sum(if(a.date>=b.date,b.num,0))
from 
(select id,date,num from cost)a 
join 
(select id,date,num from cost)b 
on (a.id = b.id )
GROUP by a.id,a.date,a.num

西瓜小王子  18:18:59
hive 中我試了,沒問題
西瓜小王子  18:20:33
但是這樣會在記憶體中製造count(*) 乘以count(*) 級數的資料

經過實驗,是可以正常運行,但由於數量級是全join,故hive執行起來也很慢。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.