Compute total, average, and medial sales amounts, grouped by Quarter and Region.

This query represents a simple cross-tabulation of the raw data.

RETURN Results AS
SELECT
  SUM(Amount) AS Total,
  AVG(Amount) AS AvgDeal,
  MEDIAN(Amount) AS MedianDeal
GROUP BY Quarter, Region

This could also be expressed using multiple statements:

RETURN Totals AS
SELECT SUM(Amount) AS Total
GROUP BY Quarter, Region ;

RETURN Avgs AS
SELECT AVG(Amount) AS AvgDeal
GROUP BY Quarter, Region ;

RETURN Medians AS
SELECT MEDIAN(Amount) AS MedianDeal
GROUP BY Quarter, Region

These queries produce the same information in different structures. For example, the first is more useful if the application is generating a single table with the total, average, and median presented in each cell. The second is more convenient if the application is generating three tables, one each for total, average, and median.

In terms of efficiency, the queries are not significantly different (the second is optimized to avoid multiple table scans). But the second returns more data (the Quarter and Region groupings must be repeated for each result), and thus is marginally less efficient.


Copyright © Legal Notices