Search code examples
sqlsql-serveraggregate-functions

Calculate the average value for each column in a table


I have a table:

Id Price Quantity Amount
1 10 2 20
2 5 5 25
3 15 2 30

I need to calculate the result as:

  • AvgPrice: The average value of column Price
  • MinQty: The minimal value of column Quantity
  • TotalAmount: sum value of column Amount

Solution

  • You can use AVG, SUM, MIN aggregate functions in SQL Server

    SELECT AVG(Price) AS AvgPrice, 
    MIN(Quantity) AS MinQty, 
    SUM(Amount) AS TotalAmount
    FROM table_name;
    

    This shall return you output as :

    AvgPrice    MinQty    TotalAmount
    ---------------------------------
    10          2         75