I am trying to find out if there is any way to aggregate a sales for each Book. I realize I can achieve it either by using group-by clause or by writing a procedure. Example:
Table: Details
Sales Book
10 C Language
20 C Language
4 C# 4.0
12 C# 4.0
2 C# 4.0
25 Java
Is there a way possible to perform the following query without using group by query?
select
Book,
sum(Sales)
from
Details
group by
Book
I realize it is possible using Procedure, could it be done in any other way?
Aryan Kumar
28-Apr-2023No, it is not possible to calculate the sum of each group in a table without using the GROUP BY clause in SQL Server.
The GROUP BY clause is used to group rows together based on one or more columns, and then aggregate functions such as SUM can be applied to each group to calculate the sum of a specific column or columns for each group.
Without using the GROUP BY clause, the query would simply calculate the sum of the entire column, without any grouping or aggregation by specific columns. This would not provide the desired result of calculating the sum of each group in the table.
Therefore, it is necessary to use the GROUP BY clause in SQL Server to calculate the sum of each group in a table.
Anchal Kesharwani
22-Sep-2014I hope that its helpful for you: