How can I group by date time column without taking time into consideration

后端 未结 8 1097
孤独总比滥情好
孤独总比滥情好 2020-11-30 17:47

I have a bunch of product orders and I\'m trying to group by the date and sum the quantity for that date. How can I group by the month/day/year without taking the time part

相关标签:
8条回答
  • 2020-11-30 18:08

    I believe you need to group by , in that day of the month of the year . so why not using TRUNK_DATE functions . The way it works is described below :

    Group By DATE_TRUNC('day' , 'occurred_at_time')
    
    0 讨论(0)
  • 2020-11-30 18:15

    GROUP BY DATE(date_time_column)

    0 讨论(0)
  • 2020-11-30 18:15

    Here's an example that I used when I needed to count the number of records for a particular date without the time portion:

    select count(convert(CHAR(10), dtcreatedate, 103) ),convert(char(10), dtcreatedate, 103)
    FROM dbo.tbltobecounted
    GROUP BY CONVERT(CHAR(10),dtcreatedate,103)
    ORDER BY CONVERT(CHAR(10),dtcreatedate,103)
    
    0 讨论(0)
  • 2020-11-30 18:22

    Cast/Convert the values to a Date type for your group by.

    GROUP BY CAST(myDateTime AS DATE)
    
    0 讨论(0)
  • 2020-11-30 18:22

    Here is the example works fine in oracle

    select to_char(columnname, 'DD/MON/yyyy'), count(*) from table_name group by to_char(createddate, 'DD/MON/yyyy');
    
    0 讨论(0)
  • 2020-11-30 18:24
    GROUP BY DATEADD(day, DATEDIFF(day, 0, MyDateTimeColumn), 0)
    

    Or in SQL Server 2008 onwards you could simply cast to Date as @Oded suggested:

    GROUP BY CAST(orderDate AS DATE)
    
    0 讨论(0)
提交回复
热议问题