Sql Min Max范围具有过功能?

时间:2017-10-18 11:49:35

标签: sql hive

我有很多用户和日期时间列。我想知道每个用户和每个月每个用户的最小值和最大值吗?

name      date        Income     Expense
Vijay     12-10-2017    10         8
Vijay     16-04-2017    25         12  

year(date) as Y_year,
month(date) as M_Month,

我尝试了下面的代码,但没有使用

min(Income)over( PARTITION by (name, Y_year,M_Month)) as min_income_of_month,
Max(Expense)over( PARTITION by (name, Y_year,M_Month)) as Max_Expense_of__month

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?

select name, year(date) as Y_year, month(date) as M_Month,
       min(income), max(income), min(expense), max(expense)
from t
group by name, year(date), month(date)
order by name, Y_year, M_Month;

答案 1 :(得分:0)

只需对要聚合的列使用min和max函数,并按用户分组和日期

无需使用窗口功能