GROUP BY Day
Query:
SELECT FORMAT(SaleDate, 'dddd') AS DayOfWeek,
SUM(QuantitySold) AS TotalQuantitySold
FROM #TempProductSales
GROUP BY FORMAT(SaleDate, 'dddd');
Output:
Explanation: In above query, we use FORMAT function to extract the day of week, allowing us to group sales by Day of week. The SUM function calculates the total quantity sold for each day of week.
How to Group by Day, Date, Hour, Month or Year in SQL Server
Grouping data by day, date, hour, month, or year in SQL Server involves using the GROUP BY clause and appropriate date functions to extract or manipulate the relevant portions of the datetime column.
The SQL Server GROUP BY operator is similar to the SQL GROUP BY Operator. The GROUP BY clause is a powerful feature for grouping rows based on unique combinations of values in specified columns. It facilitates the aggregation of data, allowing the application of aggregate functions to obtain meaningful insights.
Syntax:
SELECT column1, column2, aggregate_function(column3)
FROM table_name
GROUP BY column1, column2;
Explanation: In the above query, we used the GROUP BY clause to group rows in the result set based on unique combinations of values in columns column1 and column2. The SELECT clause then retrieves these grouped columns along with the result of applying an aggregate function aggregate_function(column3) to another column column3.