简体   繁体   中英

How to cast unix timestamp as date and extract month from it in Presto SQL

I have the following query:

    select cast(ov.ingestion_timestamp as date), date(ov.date_id), cast(ov.main_category as varchar), 
    sum(cast(ov.order_target as int)),
    sum(cast(ov.gmv_target as int))
    from tableA ov 
    inner join tableB cb
    on date(ov.date_id) = date(cb.ingestion_timestamp)
    inner join tableC loc  
    on date(ov.date_id) = date(loc.ingestion_timestamp)
    where MONTH(date(ov.ingestion_timestamp)) = month(current_date)
group by 1,2,3

在此处输入图片说明

I would like to get records where month of the ingestion_timestamp column is equals to current month.. All column values are stored as object hence I need to cast to their respective datatypes. May I know how I can retrieve month of the ingestion_timestamp column please?

Thank you.

I would suggest not cast ing in the where clause: this is inefficient, because the function needs to applied to every row before filtering.

Instead, you can compute the timestamp that corresponds to the beginning of the month, and is it for direct filtering:

where ingestion_timestamp >= to_unixtime(date_trunc(month, current_date))

If you have dates in the future you can add an upper bound limit

where 
    ingestion_timestamp >= to_unixtime(date_trunc(month, current_date))
    and ingestion_timestam < to_unixtime(date_trunc(month, current_date) + interval '1' month)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM