SQL query to group by day

后端 未结 8 2026
感动是毒
感动是毒 2020-12-04 09:21

I want to list all sales, and group the sum by day.

Sales (saleID INT, amount INT, created DATETIME)

NOTE: I am using SQL Server 2005.

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 09:44

    For PostgreSQL:

    GROUP BY to_char(timestampfield, 'yyyy-mm-dd')
    

    or using cast:

    GROUP BY timestampfield::date
    

    if you want speed, use the second option and add an index:

    CREATE INDEX tablename_timestampfield_date_idx ON  tablename(date(timestampfield));
    

提交回复
热议问题