The Basics
SELECTSEGMENT,CATEGORY,YEAR,SFROM SALES_VIEW_TBLMODELRETURN ALL ROWSPARTITION BY (SEGMENT)DIMENSION BY (CATEGORY, YEAR)MEASURES (SALES S)UNIQUE DIMENSIONRULES UPSERT SEQUENTIAL ORDER(S['Appliance', 2020] = S[CV(CATEGORY), 2019] + S[CV(CATEGORY), 2018],S['Furniture', 2020] = S[CV(CATEGORY), 2019] + S[CV(CATEGORY), 2018])ORDER BYSEGMENT,CATEGORY,YEAR;
- Partition columns. They divide the data set into logical blocks, in other words, define the boundaries for calculations. The concept is similar with partitions in the analytic functions.
- Dimension columns. They are attributes describe the measures. A full combination of them can identify one cell in a partition.
- Measure columns. These columns have numeric values and similar to facts in a multi-dimensional fact table.
RETURN ALL ROWS: This returns all selected including updated rows by the rules. RETURN UPDATED ROWS will return only those updated rows.
UNIQUE DIMENSION: This says the combination of partition and dimension columns uniquely identify one cell.
SEQUENTIAL ORDER: This means the rules will be executed in the written order in the clause.
Rules for SQL Modeling
Single Cell Reference - Constants are used for dimensions, for example, S['Appliance', 2020].
Multi-Cell Reference - Here are the two examples for the left side and right side respectively, S['Appliance', YEAR >= 2018], MAX(S)['Appliance', Year BETWEEN 1998 AND 2002].
CV Function - It provides a means to use the dimension values on the right side which are referenced on the left side as illustrated in this formula, S['Appliance', 2020] = S[CV(CATEGORY), 2019] + S[CV(CATEGORY), 2018].
UPDATE - If the referenced cell in the left side exists, its measure is updated with the calculation result. No new cells will not be inserted.
UPSERT - If the cell exists, its measure will be updated. If it doesn't exist, a new cell will be inserted.
UPSERT ALL - Simply put, it enables the model rules on the left side to have UPSERT capability. For example, S[ANY, 2020] = S[CV(), 2019] + S[CV(), 2018].
- Measures are the only updatable columns
- In terms of execution order, all other clauses, except for Select Distinct and Order By, are evaluated before the Model clause
- You can't use an aggregate or analytic function in either Select list or Order By list when you have a Model clause in your query
- You can't use subqueries in the rules except in FOR loop
- And etc.
Using Aggregate/Analytic Functions in SQL Modeling
An Example of Using an Aggregate Function
SELECTSEGMENT,CATEGORY,YEAR,SFROM SALES_VIEWWHERE YEAR = 2019MODELDIMENSION BY (SEGMENT, CATEGORY, YEAR)MEASURES (SALES S)RULES UPSERT(S['Retail', 'Sub-total', 2019] = SUM(S)[CV(SEGMENT), ANY, 2019],S['Wholesale', 'Sub-total', 2019] = SUM(S)[CV(SEGMENT), ANY, 2019],S['Total', '', 2019] = SUM(S)[ANY, 'Sub-total', 2019])ORDER BYSEGMENT,YEAR,CATEGORY;
SELECTYEAR,SEGMENT,CATEGORY,S,CSUMFROM SALES_VIEWMODELPARTITION BY ( YEAR )DIMENSION BY ( SEGMENT, CATEGORY )MEASURES (SALES S, 0 CSUM )RULES UPSERT(CSUM[ANY, ANY] = SUM(S) OVER ( PARTITION BY YEARORDER BY YEAR, SEGMENT, CATEGORYROWS UNBOUNDED PRECEDING ))ORDER BYYEAR,SEGMENT,CATEGORY;
No comments:
Post a Comment