Window-Functions

蜂巢。通過 vs where 條件進行分區

  • February 2, 2017

我有一個這樣分區的表。

# Partition Information
# col_name              data_type 

year                    string
month                   string
day                     string
hour                    string

我想在整個月內執行查詢。我明白我能做到

where day > 01 and day < 31

但是桌子很大。有人告訴我這將花費太多時間,並且我應該每天單獨執行它。我考慮過分區。喜歡

select col1, col2, sum(col3) over (partition by day)
from table

但我不確定這將如何工作。查詢是否會在每一天連續工作。在集群上會更容易嗎?col1 和 col2 會自動分組還是需要添加分組依據?

Table
col1 col2 col3 month date....
1    s    4    01    01
1    s    3    01    01
1    q    5    01    01

我想要的結果

col1 col2 col3 month date....
1    s    7    01    01
1    q    5    01    01

為了利用分區 -

您對整個月的查詢應如下所示:

select      col1,col2,sum(col3),year,month,day
from        mytale
where       year  = '2017'
       and month = '02'
group by    col1,col2,year,month,day

您在一天內的查詢應如下所示:

select      col1,col2,sum(col3),year,month,day
from        mytale
where       year  = '2017'
       and month = '02'
       and day   = '01'
group by    col1,col2,year,month,day

您在某天範圍內的查詢應如下所示:

select      col1,col2,sum(col3),year,month,day
from        mytale
where       year  = '2017'
       and month = '02'
       and day between '01' and '07'
group by    col1,col2,year,month,day

引用自:https://dba.stackexchange.com/questions/163004