Grouping Sales by Day with SQL
If you have a database table storing sales transactions, you may need to analyze sales data grouped by day. To achieve this in SQL Server 2005, you can utilize the following query:
select sum(amount) as total, dateadd(DAY,0, datediff(day,0, created)) as created from sales group by dateadd(DAY,0, datediff(day,0, created))
The above query accomplishes the grouping task using the following steps:
Example: Suppose a sale was created on '2009-11-02 06:12:55.000'. The query will extract the day component as '2009-11-02 00:00:00.000' and group the sales record under that day. It will then accumulate the amount values for all records belonging to the same day, providing you with the total sales amount for '2009-11-02'.
The above is the detailed content of How to Group Daily Sales Data Using SQL?. For more information, please follow other related articles on the PHP Chinese website!