Reporting Flashcards
Reporting
Cumulative Aggregate
TO_CHAR(sum(sum(xxx))OVER(order by yyy rows unbounded preceding), ‘999999999’) AS CUMREV
Cumulative Aggregate & Single Partition
TO_CHAR(sum(sum(xxx)))OVER(partition by yyy order by yyy rows unbounded preceding),’999999999’) AS CUMREV
Cumulative Aggregate & Multiple Partition
TO_CHAR(sum(sum(xxx)))OVER(partition by yyy order by yyy rows unbounded preceding),’999999999’)AS CUMREV1 (x2)
Moving Aggregate
TO_CHAR(avg(sum(xxx))OVER(order by yyy rows 2 preceding), ‘999999999’) AS MOV3MONTHSAVG
Cumulative - 3 key points
SUM, Rows Unbounded Preceding, Cumulative_Rev
Moving - 3 Points
AVG, Rows X Preceding, MOV_(x+1)_AVG
Rank Over
RANK()OVER(order by sum(xxx) desc) AS RANK
Rank Over With Partition
RANK()OVER(partition by yyy order by sum(xxx) desc) AS RANK_BY_PARTITION
Row Number
ROW_NUMBER()OVER(order by sum(xxx) desc) AS ROW_NUM
Dense Rank
DENSE_RANK()OVER(order by sum(xxx) desc) AS DENSE_RANK
Rank Function
Select * From ( Select AAA, BBB, SUM(xxx) AS TOTAL, RANK()OVER(...) AS RANK FROM(...) WHERE(...) GROUP BY AAA, BBB) WHERE RANK <= x
Percent Rank Function
Select dw.time_id, total, percent_rank FROM ( Select time_id, sum(xxx) AS total, Percent()rank over(...) AS perc_rank FROM fact GROUP BY time_id) t, dw WHERE t.time_id = dw.time_id AND percent_rank >= 0.9 ORDER BY percent_rank desc
Grouping and Cube
SELECT aaa, bbb, ccc, sum(xxx), GROUPING (aaa) AS aaaGroup, GROUPING (bbb) AS bbbGroup, GROUPING (ccc) AS cccGroup FROM (...) WHERE (...) GROUP BY CUBE (aaa, bbb, ccc) ORDEE BY aaa
Cube
SELECT aaa, bbb, ccc, sum(xxx) FROM (...) WHERE (...) GROUP BY CUBE (aaa, bbb, ccc) ORDER BY aaa
Partial Cube
SELECT aaa, bbb, ccc, sum(xxx) FROM (...) WHERE (...) GROUP BY CUBE (aaa, bbb),ccc ORDER BY aaa