如何在 hive 中使用强制转换列作为 where 子句条件?
例如,“dt”是一个字符串,我将其转换为日期类型,然后我想将其用作 WHERE 子句中的条件,但失败:
hive> select mid, cast(to_date(from_unixtime(unix_timestamp(dt, 'yyyyMMdd'))) as date) from message_use_tags where date<2021-11-08 and date >2021-11-01 limit 100;
FAILED: SemanticException Line 0:-1 Invalid table alias or column reference 'date': (possible column names are: mid, type, content, dt)
我已经将“dt”转换为“日期”,然后如何在 WHERE 子句中使用它?
For example, the 'dt' is a string and I cast it to a date type, then I want to use it as a condition in WHERE clause, but it failed:
hive> select mid, cast(to_date(from_unixtime(unix_timestamp(dt, 'yyyyMMdd'))) as date) from message_use_tags where date<2021-11-08 and date >2021-11-01 limit 100;
FAILED: SemanticException Line 0:-1 Invalid table alias or column reference 'date': (possible column names are: mid, type, content, dt)
I already convert 'dt' to 'date', and then how to use it in the WHERE clause?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在 select 中所做的转换不适用于 where 子句,否则您必须在子查询中设置转换并在外部查询中应用过滤,或者您可以尝试这个
the casting you did in the select wouldnt be applicable it the where clause else you would have to set the casting in a subquery and apply the filtering in the outer query, or you could try this
请尝试下面的代码。有时自动转换对于某些工具不起作用。更好地转换和信任代码而不是工具。
请注意,我假设列 dt 是格式为 yyyyMMdd 的字符串日期。
Please try below code. Sometime auto conversion doesnt work for some tools. Better convert and trust the code and not the tool.
Pls note, i assumed column
dt
to be a string date in the format yyyyMMdd.