Variables can also be used to emulate analytic functions. A few examples follow. For simplicity, let’s assume all data fields NOT NULL, and sorting and partitioning (PARTITION BY) use one column only. Including NULLs and multi-column sorting/partitioning would make the examples more cumbersome and difficult to read without changing their basic idea.
Let’s create a table named TestTable for our examples:
CREATETABLE TestTable(
group_id INT NOTNULL,
order_id INT UNIQUENOTNULL,
value INT NOTNULL
);
Here, group_id is the group identifier (the equivalent of an analytic function window), order_id contains unique values used for sorting, and value is just a numeric value.