Jed Rembold
Monday, October 20, 2025
Assuming that your server has its timezone set to
'American/Los_Angeles', what is the output of
the below query if you ran it right now?
SELECT make_time(
date_part('dow', current_date)::INT,
date_part('day', now())::INT,
date_part('hour', localtime)::INT
) AT TIME ZONE 'PDT';
SELECT or ORDER BY
statements
OVER () keyword, which comes after the
aggregating window function() determines the
“window” of the window functionSELECT AVG(|||column|||) OVER()
FROM |||table|||;| Function | Description |
|---|---|
row_number() |
Assigns an ascending row number to each row in a window |
rank() |
Assigns an ascending rank to each row, with possible ties skipping the next value |
dense_rank() |
Assigns an ascending rank to each row, with possible ties not skipping the next value |
first_value(|||col|||) |
Returns the first value in the window of column
|||col||| |
last_value(|||col|||) |
Returns the last value in the window of column
|||col||| |
lag(|||col|||, |||amt|||) |
Returns the previous (or shifted by
|||amt|||) row of column
|||col||| |
lead(|||col|||, |||amt|||) |
Returns the next (or shifted by
|||amt|||) row of column
|||col||| |
nth_value(|||col|||, |||n|||) |
Returns the |||n|||th row of column
|||col||| (NULL if
doesn’t exist) |
OVER() statementSELECT COUNT(*) OVER( ORDER BY |||column||| )
FROM |||table|||;
You can tweak this window by specifying the starting and stopping point, using the syntax:
...|||type||| BETWEEN |||offset||| PRECEDING AND |||offset||| FOLLOWING
which appears in the OVER clause, after
any provided ordering
|||type||| can be either
ROWS, RANGE, or
GROUP|||offset||| can be
UNBOUNDED if the type is
ROWS or GROUPRANGECan also exclude the current row or group from the window:
... EXCLUDE |||type||||||type||| here can be:
CURRENT ROW, which excludes the current
row from the windowGROUP which excludes the current row and
all other rows currently tied with itTIES which just excludes the tied rows,
but keeps the current rowGROUP BY, but
every row will get a value hereSELECT AVG(|||column|||) OVER (
PARTITION BY |||column|||
)
FROM |||table|||;