按SQL / Hive分组

时间:2018-10-11 09:18:08

标签: sql hive

我正在尝试一种有效的方式来在配置单元中运行查询。

考虑到我有2列:event, date_in_ist

date_in_ist的字符串格式为:yyyy-mm-dd

我正在寻找以下格式的每月唯一身份用户数:(事件应该以({"app_opened","app_access","app_access"

Event   Month-Year  Count_Users
 a       09-2017       50
 a       02-2017       100
 b       09-2018       40 

我们将不胜感激

3 个答案:

答案 0 :(得分:0)

尝试以下

linearLayout2.setVisibility(View.GONE); linearLayout3.setVisibility(View.GONE); 

答案 1 :(得分:0)

使用'yyyy-MM-dd'模板,而不使用'yyyy-mm-dd'

from_unixtime(unix_timestamp(date_in_ist,'yyyy-MM-dd'), 'MM-yyyy') as 'Month-Year'

或者您可以使用concat_ws和substr:

select event,
       concat_ws('-',substr(date_in_ist,6,2),substr(date_in_ist,1,4)) as 'Month-Year',
       count(*) as count_users
from tablename
where eveent in ('app_opened','app install','app_access','app launched')
group by event,
         concat_ws('-',substr(date_in_ist,6,2),substr(date_in_ist,1,4))

答案 2 :(得分:0)

我将日期返回为yyyy-mm,并使用简单的汇总:

select event, substr(date_in_ist, 1, 7) as yyyy_mm
       count(distinct user_id) as count_users
from t
where event in ('app_opened','app install','app_access','app launched')
group by event, substr(date_in_ist, 1, 7);