一、Quick BI中的lod_ include函数
lod_ include {维度1[,维度2]...:聚合表达式[:过滤条件]}
作用:将表达式中的维度一起作为分组依据进行订算。其中,
1)??? 维度1[,维度2]... :声明维度,指定聚合表达式要连接到的一个或多个维度。使用逗号分隔各个维度。
2)??? 聚合表达式:聚合表达式是所执行的计算,用于定义目标维度。
3)??? 过滤条件:可以通过对前面的声明维度设定条件进行过滤,非必须。
二、问题描述
在Quick BI数据集新建计算字段“卷烟订单数量= lod_include{[是否卷烟],[类型]:COUNT(distinct [流水号]):[是否卷烟]='卷烟',[类型]='零售单'}”。
注:在新建字段时lod_include函数不能换行,否则会出错。
在Quick BI仪表板中添加指标图计算,结果报错:
三、问题分析
查看SQL代码
SELECT
? LOD_TP_0.`LOD_61D3EE39D3B393` AS LOD_0
FROM
? (
??? SELECT
????? '-' AS T_ALA_2_
??? FROM
????? `terminaldata2`.`sales` AS ASE_T_1_
??? LIMIT
????? 0, 1
? ) AS LOD_TM
? CROSS JOIN (
??? SELECT
????? SUM(LOD_TR.`LOD_measure_result`) AS LOD_61D3EE39D3B393
??? FROM
????? (
??????? SELECT
????????? AST_T_3_.`是否卷烟` AS LOD_280795B8AB3CAB,
????????? ASE_T_1_.`类型` AS LOD_601A3BCEB54372
??????? FROM
????????? `terminaldata2`.`sales` AS ASE_T_1_
????????? LEFT JOIN `terminaldata2`.`products` AS AST_T_3_ ON ASE_T_1_.`商品条码` = AST_T_3_.`商品条码`
??????? GROUP BY
????????? AST_T_3_.`是否卷烟`,
????????? ASE_T_1_.`类型`
????? ) AS LOD_TL
????? INNER JOIN (
??????? SELECT
????????? AST_T_3_.`是否卷烟` AS LOD_280795B8AB3CAB,
????????? COUNT(
??????????? distinct ASE_T_1_.`流水号`
????????? ) AS LOD_measure_result,
????????? ASE_T_1_.`类型` AS LOD_601A3BCEB54372
??????? FROM
????????? `terminaldata2`.`sales` AS ASE_T_1_
????????? LEFT JOIN `terminaldata2`.`products` AS AST_T_3_ ON ASE_T_1_.`商品条码` = AST_T_3_.`商品条码`
??????? WHERE
????????? AST_T_3_.`是否卷烟` = '卷烟',
????????? ASE_T_1_.`类型` = '零售单'
??????? GROUP BY
????????? AST_T_3_.`是否卷烟`,
????????? ASE_T_1_.`类型`
????? ) AS LOD_TR ON LOD_TL.`LOD_280795B8AB3CAB` = LOD_TR.`LOD_280795B8AB3CAB`
????? AND LOD_TL.`LOD_601A3BCEB54372` = LOD_TR.`LOD_601A3BCEB54372`
? ) AS LOD_TP_0
LIMIT
? 0, 1
在上述代码中第73~77行where子句两个过滤条件用逗号连接,显然是错误的。WHERE子句后面的过滤条件之间应用‘AND‘连接。
四、优化建议
将where子句两个过滤条件间的逗号改为‘AND’。即将WHERE?? AST_T_3_.`是否卷烟` = '卷烟', ASE_T_1_.`类型` = '零售单' 改为 WHERE?? AST_T_3_.`是否卷烟` = '卷烟' AND ASE_T_1_.`类型` = '零售单'
五、总结
WHERE子句后面有多个过滤条件时,应用AND或OR连接。