如何按CONCAT_WS分组

时间:2019-02-13 08:09:57

标签: sql hive

我试图将concat_ws函数运行到group by中,但出现以下错误。是否意味着Hive不支持将concat_ws集成到group by中?如果没有,还有其他写方法吗?

在Waterfall_table中有以下记录(仅包含年,月,日):

Year, Month, Date, 
2018, 08, 09 
2019, 09, 27
2017, 09, 27
2019, 02, 27
2019, 01, 27
2019, 01, 30
2019, 09, 27
2017, 09, 27
2019, 02, 27
2019, 01, 27
2019, 01, 30
..., ..., ...

有没有一种方法可以使用查询将我的记录分为几行,从而将年,月和日全部分组?

查询的最终结果将是两行:

realdate,num
2019-01-27, 4
2019-01-28, 23
2019-01-29, 34
2019-02-01, 8
2019-02-02, 4

我认为查询应如下所示:

    select
        concat_ws('-', year, month, day) as realdate,count(*)
    from
        waterfall_table
    where
        concat_ws('-', year, month, day) between '2019-01-25' and '2019-02-10'
    group by  concat_ws('-', year, month, day)
    order by concat_ws('-', year, month, day) desc
    limit 100

5 个答案:

答案 0 :(得分:0)

请尝试这个

select
  cast( ltrim( year) +'-'+ltrim( month) +'-'+ltrim( day ) as date),count(*)
from
    (select 2018 year ,10 month,25 day union select 2017 year ,11 month,10 
    union select 2016 year ,8 month,14 )t1
where
  cast( ltrim( year) +'-'+ltrim( month) +'-'+ltrim( day ) as date) between cast('2017-01-25' as date) and  cast('2019-02-10' as date)
 group by  year,month,day
order by  cast( ltrim( year) +'-'+ltrim( month) +'-'+ltrim( day ) as date) desc

答案 1 :(得分:0)

如果我正确理解,则需要类似以下内容的

SELECT CAST( CONCAT(`YEAR`, '-', `MONTH`, '-', `DATE` ) AS DATE ) AS realdate, COUNT(*) as num
FROM your_table
GROUP BY realdate

答案 2 :(得分:0)

您可以在GROUP BY和ORDER BY子句中使用列别名

SELECT CONCAT_WS('-', year, month, day) as realdate, COUNT(*)
FROM waterfall_table
WHERE CONCAT_WS('-', year, month, day) BETWEEN '2019-01-25' AND '2019-02-10'
GROUP BY realdate
ORDER BY realdate DESC
LIMIT 100

答案 3 :(得分:0)

Hive对别名很挑剔。试试这个:

按实时降序排列

答案 4 :(得分:0)

在ORDER BY中使用别名:

with waterfall_table as
(
select stack(11,
'2018', '08', '09', 
'2019', '09', '27',
'2017', '09', '27',
'2019', '02', '27',
'2019', '01', '27',
'2019', '01', '30',
'2019', '09', '27',
'2017', '09', '27',
'2019', '02', '27',
'2019', '01', '27',
'2019', '01', '30') as (Year, Month, day)
)

select
        concat_ws('-', year, month, day) as realdate, count(*) as cnt
    from
        waterfall_table
    where
        concat_ws('-', year, month, day) between '2019-01-25' and '2019-02-10'
    group by  concat_ws('-', year, month, day)
    order by realdate desc
    limit 100;

结果:

OK
realdate        cnt
2019-01-30      2
2019-01-27      2
Time taken: 92.909 seconds, Fetched: 2 row(s)

此吉拉似乎与以下人物有关:here