【问题标题】:How to GROUP BY the CONCAT_WS如何按 CONCAT_WS 分组
【发布时间】:2020-10-28 16:06:33
【问题描述】:

我正在尝试将 concat_ws 函数运行到 group by 中,但出现以下错误。是否意味着 Hive 不支持将concat_ws 转换为group by?如果没有,有没有其他的写法?

我在瀑布表中有以下记录(只有年月日):

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

【问题讨论】:

  • 更新您的问题并添加适当的数据样本和预期结果......并显示确切的错误消息
  • 使用一列添加完整日期以提高查询性能
  • 为什么 2019-01-27 有 num: 4 ?您的示例数据中只有两个日期2019-01-27

标签: sql hive concatenation


【解决方案1】:

请试试这个

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您可以在 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
      

      【讨论】:

        【解决方案4】:

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

        按实时顺序排序

        【讨论】:

          【解决方案5】:

          在 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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-06-06
            • 2015-06-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多