Microsoft SQL Server Aggregated Query
Microsoft SQL Server supports many aggregation query operations, such as SUM, COUNT, AVG, MIN, MAX, and so on. The following are examples of various aggregation queries, assuming the table name is "Orders" and the table structure is as follows:
Orders table:
OrderID | Customer Name | Product Name | Quantity | Price
--------------------------------------------------------------
1 | John Smith | Product A | 10 | 5.99
2 | Jane Doe | Product B | 5 | 8.99
3 | John Smith | Product C | 2 | 12.99
4 | Jane Doe | Product A | 7 | 5.99
5 | John Smith | Product B | 3 | 8.99
1. SUM aggregation query:
sql
SELECT SUM(Quantity) as TotalQuantity
FROM Orders;
Output:
TotalQuantity
-------------
27
2. Count aggregation query:
sql
SELECT COUNT(*) as TotalOrders
FROM Orders;
Output:
TotalOrders
-----------
5
3. AVG aggregation query:
sql
SELECT AVG(Price) as AveragePrice
FROM Orders;
Output:
AveragePrice
------------
8.91
4. Min aggregation query:
sql
SELECT MIN(Quantity) as MinQuantity
FROM Orders;
Output:
MinQuantity
-----------
2
5. Maximum Aggregate Query:
sql
SELECT MAX(Price) as MaxPrice
FROM Orders;
Output:
MaxPrice
--------
12.99
6. GROUP BY aggregation query:
sql
SELECT CustomerName, SUM(Quantity) as TotalQuantity
FROM Orders
GROUP BY CustomerName;
Output:
Customer Name | TotalQuantity
-------------------------------
John Smith | 15
Jane Doe | 12
The above are some common examples of aggregation queries. SQL Server also supports other complex aggregation query operations, such as JOIN and SUBQUERY, which can be further combined and filtered according to specific needs.