Group by day from timestamp

前端 未结 3 1553
名媛妹妹
名媛妹妹 2020-12-23 12:29

In my posts table, it saves timestamp for each post record.

What query can group posts by day, using this timestamp column?

相关标签:
3条回答
  • 2020-12-23 12:59
    SELECT DATE(timestamp) AS ForDate,
            COUNT(*) AS NumPosts
     FROM   user_messages
     GROUP BY DATE(timestamp)
     ORDER BY ForDate
    

    This found for me. I have timestamp like "2013-03-27 15:46:08".

    0 讨论(0)
  • 2020-12-23 13:07

    How about this? Using the DATE function:

     SELECT DATE(FROM_UNIXTIME(MyTimestamp)) AS ForDate,
            COUNT(*) AS NumPosts
     FROM   MyPostsTable
     GROUP BY DATE(FROM_UNIXTIME(MyTimestamp))
     ORDER BY ForDate
    

    This will show you the number of posts on each date that the table has data for.

    0 讨论(0)
  • 2020-12-23 13:19
    SELECT
        *
    FROM
    (
        SELECT DATE(FROM_UNIXTIME(MyTimestamp)) AS ForDate,
               ROW_NUMBER() OVER (PARTITION BY DATE(FROM_UNIXTIME(MyTimestamp)) ORDER BY MyTimeStamp) AS PostRowID,
               *
        FROM   MyPostsTable
    )
        AS sequenced_daily_posts
    WHERE
        ForDate = <whatever date(s) you want>
        AND PostRowID <= 2
    
    0 讨论(0)
提交回复
热议问题