如何按CONCAT_WS分组

问题描述 投票:0回答:5

我试图将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
sql hive
5个回答
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

0
投票

如果我正确理解,你需要这样的东西:

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

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

0
投票

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

通过realdate desc订购


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)

这个Jira似乎有关:HIVE-1449

© www.soinside.com 2019 - 2024. All rights reserved.