LIB CONNECT TO 'CustomSQL:Snowflake_example.com';
Section DirectQuery;
C:
Select C_NATIONKEY, Sum(C_ACCTBAL) as C_SUM from tpch1.customer group by C_NATIONKEY;
R:
SELECT R_REGIONKEY, R_NAME from tpch1.region;
N:
SELECT N_REGIONKEY, N_NAME, N_NATIONKEY from tpch1.nation;
Create Relationship Between R, N Matching R_REGIONKEY With N_REGIONKEY;
Create Relationship
Between Outer Joined N, Inner Joined C On
(coalesce([N_NATIONKEY], -1) = coalesce([C_NATIONKEY], -1));
示例:将具有自定义 SQL 的表添加到由 数据模型管理器 创建的数据模型中
在本例中,首先使用 DirectQueryModel.main 对象中的内容构建 Direct Query 数据模型,该对象是使用 数据模型管理器 创建的。然后,将一个包含字段 C_CUSTKEY 和 C_ACCTBAL 的 C 附加表添加到数据模型中。ORDERS 中的字段 O_CUSTKEY 与 C 中的字段 C_CUSTKEY 之间建立了关系。
LIB CONNECT TO 'CustomSQL:Snowflake_example.com';
// DMM created model portion includes Orders table.
IMPORT LIVE 'ModelName@obj://DirectQueryModel.Main';
Section DirectQuery;
C: Select C_CUSTKEY, C_ACCTBAL from tpch1.customer;
Create Relationship Between ORDERS, C on (O_CUSTKEY = C_CUSTKEY);
示例:使用自定义 SQL 表构建 Direct Query 数据模型
在本例中, Direct Query 数据模型是使用自定义 SQL 表 C 构建的,其中包含重命名的字段 C_KEY 和 C_BAL。接下来,IMPORT LIVE 添加在来自 DirectQueryModel.main 的 数据模型管理器 中创建的数据模型。此数据模型包含订单表,并且在该模型中的 Orders 和 Customer 之间具有现有关系。然后,删除 Customers 和 Orders 之间的现有关系,并在 Orders 和 C 之间定义新的关系。
LIB CONNECT TO 'CustomSQL:Snowflake_example.com';
Section DirectQuery;
C: Select C_CUSTKEY as C_KEY, C_ACCTBAL as C_BAL from tpch1.customer;
// DMM created model portion includes Orders table.
IMPORT LIVE 'ModelName@obj://DirectQueryModel.Main';
// This relationship was defined using DMM between ORDERS and CUSTOMER, but I want to define my own.
Drop Relationship Between ORDERS, CUSTOMER;
Create Relationship Between ORDERS, C on (O_CUSTKEY = C_KEY);
示例:构建包含变量的数据模型
在本例中,变量 MULT 首先使用值 100 定义。然后,将其作为乘法器应用于 PS_Supplycost 的总和,以计算字段 S。
LIB CONNECT TO 'CustomSQL:Snowflake_example.com';
Section DirectQuery;
LET MULT=100;
T1: SELECT PS_AVAILQTY AS C, SUM(PS_SUPPLYCOST) * $(MULT) AS S FROM "TPCH.01"."PARTSUPP" GROUP BY C;
IMPORT LIVE 'ModelName@obj://DirectQueryModel.Main';
section DirectQuery;
let Aggr1 = 1;
let Aggr2 = 0;
[GROUPS]:
SELECT 0 as GROUP_CODE, 'NONE' as "GROUP"
UNION ALL
SELECT 1 as GROUP_CODE, 'ORDERSTATUS' as "GROUP"
UNION ALL
SELECT 2 as GROUP_CODE, 'ORDERPRIORITY' as "GROUP"
;
[ORDERS_AGGREGATIONS]:
SELECT 'BY ORDERSTATUS' as "GROUP_NAME",
"O_ORDERSTATUS" as "ORDERS_GROUP",
sum("O_TOTALPRICE") as "ORDERS_GROUP_TOTAL_PRICE"
FROM "TEST1"."TPCH.01"."ORDERS"
WHERE '$(=min({<GROUP_CODE={0,1}>}[GROUP_CODE]))' = '1'
GROUP BY 1,2
UNION ALL
SELECT 'BY ORDERPRIORITY' as "GROUP_NAME",
"O_ORDERPRIORITY" as "ORDERS_GROUP",
sum("O_TOTALPRICE") as "ORDERS_GROUP_TOTAL_PRICE"
FROM "TEST1"."TPCH.01"."ORDERS"
WHERE '$(=min({<GROUP_CODE={0,2}>}[GROUP_CODE]))' = '2'
GROUP BY 1,2;
;
[ORDERS_AGGREGATIONS_VARS]:
SELECT 'BY ORDERSTATUS' as "GROUP_NAME_V",
"O_ORDERSTATUS" as "ORDERS_GROUP_V",
sum("O_TOTALPRICE") as "ORDERS_GROUP_TOTAL_PRICE_V"
FROM "TEST1"."TPCH.01"."ORDERS"
WHERE $(=Aggr1) = 1
GROUP BY 1,2
UNION ALL
SELECT 'BY ORDERPRIORITY' as "GROUP_NAME_V",
"O_ORDERPRIORITY" as "ORDERS_GROUP_V",
sum("O_TOTALPRICE") as "ORDERS_GROUP_TOTAL_PRICE_V"
FROM "TEST1"."TPCH.01"."ORDERS"
WHERE $(=Aggr2) = 1
GROUP BY 1,2;
;