连接后如何转换为日期 data_type?

How do I convert to date data_type after concatenating?

我有一个查询,我要连接月份和年份并将其保存为 MY,然后将其转换为字符串。需要帮助在连接后从字符串转换为日期 data_type。

select concat (month(date),'-', year(date)) as MY, post_evar10, device_type, sum(pageviews) as pageviews, count(distinct uniquevisitors) as uniquevisitors
from temp.MS_Adobe_Discover1
group by MY, post_evar10, device_type
order by MY asc;

如果你想在 Sparksql 中转换为日期:

select to_date(concat(year(date), '-', month(date), '-01')) as yyyymmdd,
       post_evar10, device_type,
       sum(pageviews) as pageviews, count(distinct uniquevisitors) as uniquevisitors
from temp.MS_Adobe_Discover1
group by yyyymmdd, post_evar10, device_type
order by yyyymmdd asc;

没有像“2019-10”或“10-2019”这样的日期类型格式。

我们可以从日期类型获取此字符串,但无法将字符串从“2019-10”或“10-2019”转换为日期数据类型。它会给出错误:

如果我们要进行这种转换,我们必须为 Gordon 为您展示的字符串添加一个 'DD'。

或者请不要将其转换为date数据类型,仍然使用varchar数据类型。与您的查询略有不同:

select concat (year(date),'-', month(date)) as MY, post_evar10, device_type, sum(pageviews) as pageviews, count(distinct uniquevisitors) as uniquevisitors
from temp.MS_Adobe_Discover1
group by MY, post_evar10, device_type
order by MY asc;

希望对您有所帮助。