美文网首页
在SQL中处理timastamp数据

在SQL中处理timastamp数据

作者: fishgor | 来源:发表于2018-05-09 23:09 被阅读19次

    针对timestamp数据,可以使用DATE_TRUNC或者DATE_PART来截取时间元素。
    DATE_TRUNC
    https://blog.modeanalytics.com/date-trunc-sql-timestamp-function-count-on/

    SELECT DATE_TRUNC('day', occurred_at) AS day,
           user_id
      FROM benn.fake_fact_events
     WHERE event_name = 'complete_signup'
       AND occurred_at >= '2014-03-10'
       AND occurred_at <= '2014-05-26'
     ORDER BY 1 DESC
    

    DATE_PART
    http://www.postgresqltutorial.com/postgresql-date_part/

    SELECT DATE_PART('dow', occurred_at) day_of_week,
           SUM(total) total_qty
      FROM orders
     GROUP BY 1
     ORDER BY 2 DESC;
    

    相关文章

      网友评论

          本文标题:在SQL中处理timastamp数据

          本文链接:https://www.haomeiwen.com/subject/eerarftx.html