CodingDatabases One can use the following operators to perform operations across an entire column/subset of a column (window):
SUM- sum all values in a column/windowMIN- return the minimum of all values in a column/windowMAX- return the minimum of all values in a column/windowAVG- averages all values in a column/windowCOUNT- counts all rows are in a column/windowFIRST_VALUE()- returns the first value in the column/windowLAST_VALUE()- returns the last value in the column/window
These operations can act on subsets of rows by three methods:
GROUP BY- operates on rows with the same values for particular columnsHAVING- filter aggregated dataOVER- specify that the aggregation will occur over a window which can be specified using aPARTITION BY columnclause These operators are used in theSELECTclause of a query:
SELECT column1,
MIN(column1) AS min_col1,
MAX(column1) AS max_col1,
SUM(column1) OVER (
PARTITION BY column2
ORDER BY column1) AS window_sum,
FROM table
GROUP BY column1
HAVING min_col1 >= L AND max_col1 <= U;
One can also use COUNT on the data returned by a CASE statement used for conditionally filtering data:
SELECT
COUNT(CASE
WHEN condition THEN 1
ELSE NULL
END) AS new_count
FROM table;