一、Quick BI中的lod_ include函数
lod_ include {维度1[,维度2]...:聚合表达式[:过滤条件]}
作用:将表达式中的维度一起作为分组依据进行订算。其中,
1) 维度1[,维度2]... :声明维度,指定聚合表达式要连接到的一个或多个维度。使用逗号分隔各个维度。
2) 聚合表达式:聚合表达式是所执行的计算,用于定义目标维度。
3) 过滤条件:可以通过对前面的声明维度设定条件进行过滤,非必须。
二、、问题描述
3月 | 4月 | 5月 | 合计 | |
总结存数量 | 35116176 | 36286140 | 37120427 | 108522743 |
在Quick BI数据集新建计算字段” 总结存数量=CASE WHEN MAX([月份])='3月' THEN lod_include{[月份]:SUM([结存数量]):[月份]='3月'} WHEN MAX([月份])='4月' THEN lod_include{[月份]:SUM([结存数量]):[月份]='4月'} WHEN MAX([月份])='5月' THEN lod_include{[月份]:SUM([结存数量]):[月份]='5月'} 。
END”
在Quick BI仪表板新建交叉表,以‘月份’为行,以‘总结存数量’为列,执行结果“数据返回为空“,这个结果显示不正确。
三、问题分析
查看SQL代码
SELECTLOD_TM.`LOD_2C07D23FC7BBAA` AS LOD_0,CASEWHEN LOD_TM.`temp_calculation_0` = '3月' THEN LOD_TP_0.`LOD_C5682C34D69BEC`WHEN LOD_TM.`temp_calculation_0` = '4月' THEN LOD_TP_1.`LOD_D78F97D23AEDC1`WHEN LOD_TM.`temp_calculation_0` = '5月' THEN LOD_TP_2.`LOD_5B3771E3CF7947`END AS LOD_1FROM(SELECTATN_T_1_.`月份` AS LOD_2C07D23FC7BBAA,MAX(ATN_T_1_.`月份`) AS temp_calculation_0FROM`terminaldata2`.`account` AS ATN_T_1_GROUP BYATN_T_1_.`月份`ORDER BYLOD_2C07D23FC7BBAA ASCLIMIT0, 20) AS LOD_TMINNER JOIN (SELECTLOD_TL.`LOD_2C07D23FC7BBAA` AS LOD_2C07D23FC7BBAA,LOD_TR.`LOD_measure_result` AS LOD_C5682C34D69BECFROM(SELECTATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_GROUP BYATN_T_1_.`月份`) AS LOD_TLINNER JOIN (SELECTSUM(ATN_T_1_.`结存数量`) AS LOD_measure_result,ATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_WHEREATN_T_1_.`月份` = '3月'GROUP BYATN_T_1_.`月份`) AS LOD_TR ON LOD_TL.`LOD_2C07D23FC7BBAA` = LOD_TR.`LOD_2C07D23FC7BBAA`GROUP BYLOD_TL.`LOD_2C07D23FC7BBAA`) AS LOD_TP_0 ON LOD_TM.`LOD_2C07D23FC7BBAA` = LOD_TP_0.`LOD_2C07D23FC7BBAA`INNER JOIN (SELECTLOD_TL.`LOD_2C07D23FC7BBAA` AS LOD_2C07D23FC7BBAA,LOD_TR.`LOD_measure_result` AS LOD_D78F97D23AEDC1FROM(SELECTATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_GROUP BYATN_T_1_.`月份`) AS LOD_TLINNER JOIN (SELECTSUM(ATN_T_1_.`结存数量`) AS LOD_measure_result,ATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_WHEREATN_T_1_.`月份` = '4月'GROUP BYATN_T_1_.`月份`) AS LOD_TR ON LOD_TL.`LOD_2C07D23FC7BBAA` = LOD_TR.`LOD_2C07D23FC7BBAA`GROUP BYLOD_TL.`LOD_2C07D23FC7BBAA`) AS LOD_TP_1 ON LOD_TM.`LOD_2C07D23FC7BBAA` = LOD_TP_1.`LOD_2C07D23FC7BBAA`INNER JOIN (SELECTLOD_TL.`LOD_2C07D23FC7BBAA` AS LOD_2C07D23FC7BBAA,LOD_TR.`LOD_measure_result` AS LOD_5B3771E3CF7947FROM(SELECTATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_GROUP BYATN_T_1_.`月份`) AS LOD_TLINNER JOIN (SELECTSUM(ATN_T_1_.`结存数量`) AS LOD_measure_result,ATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_WHEREATN_T_1_.`月份` = '5月'GROUP BYATN_T_1_.`月份`) AS LOD_TR ON LOD_TL.`LOD_2C07D23FC7BBAA` = LOD_TR.`LOD_2C07D23FC7BBAA`GROUP BYLOD_TL.`LOD_2C07D23FC7BBAA`) AS LOD_TP_2 ON LOD_TM.`LOD_2C07D23FC7BBAA` = LOD_TP_2.`LOD_2C07D23FC7BBAA`SELECTCOUNT(1) AS query_countFROM(SELECTLOD_TM.`LOD_2C07D23FC7BBAA` AS LOD_0,CASEWHEN LOD_TM.`temp_calculation_0` = '3月' THEN LOD_TP_0.`LOD_C5682C34D69BEC`WHEN LOD_TM.`temp_calculation_0` = '4月' THEN LOD_TP_1.`LOD_D78F97D23AEDC1`WHEN LOD_TM.`temp_calculation_0` = '5月' THEN LOD_TP_2.`LOD_5B3771E3CF7947`END AS LOD_1FROM(SELECTATN_T_1_.`月份` AS LOD_2C07D23FC7BBAA,MAX(ATN_T_1_.`月份`) AS temp_calculation_0FROM`terminaldata2`.`account` AS ATN_T_1_GROUP BYATN_T_1_.`月份`) AS LOD_TMINNER JOIN (SELECTLOD_TL.`LOD_2C07D23FC7BBAA` AS LOD_2C07D23FC7BBAA,LOD_TR.`LOD_measure_result` AS LOD_C5682C34D69BECFROM(SELECTATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_GROUP BYATN_T_1_.`月份`) AS LOD_TLINNER JOIN (SELECTSUM(ATN_T_1_.`结存数量`) AS LOD_measure_result,ATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_WHEREATN_T_1_.`月份` = '3月'GROUP BYATN_T_1_.`月份`) AS LOD_TR ON LOD_TL.`LOD_2C07D23FC7BBAA` = LOD_TR.`LOD_2C07D23FC7BBAA`GROUP BYLOD_TL.`LOD_2C07D23FC7BBAA`) AS LOD_TP_0 ON LOD_TM.`LOD_2C07D23FC7BBAA` = LOD_TP_0.`LOD_2C07D23FC7BBAA`INNER JOIN (SELECTLOD_TL.`LOD_2C07D23FC7BBAA` AS LOD_2C07D23FC7BBAA,LOD_TR.`LOD_measure_result` AS LOD_D78F97D23AEDC1FROM(SELECTATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_GROUP BYATN_T_1_.`月份`) AS LOD_TLINNER JOIN (SELECTSUM(ATN_T_1_.`结存数量`) AS LOD_measure_result,ATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_WHEREATN_T_1_.`月份` = '4月'GROUP BYATN_T_1_.`月份`) AS LOD_TR ON LOD_TL.`LOD_2C07D23FC7BBAA` = LOD_TR.`LOD_2C07D23FC7BBAA`GROUP BYLOD_TL.`LOD_2C07D23FC7BBAA`) AS LOD_TP_1 ON LOD_TM.`LOD_2C07D23FC7BBAA` = LOD_TP_1.`LOD_2C07D23FC7BBAA`INNER JOIN (SELECTLOD_TL.`LOD_2C07D23FC7BBAA` AS LOD_2C07D23FC7BBAA,LOD_TR.`LOD_measure_result` AS LOD_5B3771E3CF7947FROM(SELECTATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_GROUP BYATN_T_1_.`月份`) AS LOD_TLINNER JOIN (SELECTSUM(ATN_T_1_.`结存数量`) AS LOD_measure_result,ATN_T_1_.`月份` AS LOD_2C07D23FC7BBAAFROM`terminaldata2`.`account` AS ATN_T_1_WHEREATN_T_1_.`月份` = '5月'GROUP BYATN_T_1_.`月份`) AS LOD_TR ON LOD_TL.`LOD_2C07D23FC7BBAA` = LOD_TR.`LOD_2C07D23FC7BBAA`GROUP BYLOD_TL.`LOD_2C07D23FC7BBAA`) AS LOD_TP_2 ON LOD_TM.`LOD_2C07D23FC7BBAA` = LOD_TP_2.`LOD_2C07D23FC7BBAA`) AS a
以WHEN MAX([月份])='3月' THEN lod_include{[月份]:SUM([结存数量]):[月份]='3月'}为例分析:
上述代码,中LOD_TM表结果为
LOD_2C07D23FC7BBAA | temp_calculation_0 |
3月 | 3月 |
4月 | 4月 |
5月 | 5月 |
LOD_TL表结果为
LOD_2C07D23FC7BBAA |
3月 |
4月 |
5月 |
LOD_TR表结果为
LOD_measure_result | LOD_2C07D23FC7BBAA |
35116176 | 3月 |
LOD_TL与LOD_TR通过条件”ON LOD_TL.`LOD_2C07D23FC7BBAA` = LOD_TR.`LOD_2C07D23FC7BBAA` GROUP BY LOD_TL.`LOD_2C07D23FC7BBAA`”进行内连接(INNER JOIN)得到表LOD_TP_0,LOD_TP_0结果为:
LOD_2C07D23FC7BBAA | LOD_C5682C34D69BEC |
3月 | 35116176 |
LOD_TM与LOD_TP_0通过条件“ON LOD_TM.`LOD_2C07D23FC7BBAA` = LOD_TP_0.`LOD_2C07D23FC7BBAA` “进行内连接(INNER JOIN),得到的结果为(这里称为表A1,由于是内连接,所以4月、5月无法显示):
LOD_2C07D23FC7BBAA | temp_calculation_0 | LOD_C5682C34D69BEC(LOD_TP_0) |
3月 | 3月 | 35116176 |
同理,当“WHEN MAX([月份])='4月' THEN lod_include{[月份]:SUM([结存数量]):[月份]='4月'}“,得到的结果为(这里称为表A2,由于是内连接,所以3月、5月无法显示):
LOD_2C07D23FC7BBAA | temp_calculation_0 | LOD_C5682C34D69BEC(LOD_TP_1) |
4月 | 4月 | 36286140 |
同理,当“WHEN MAX([月份])='5月' THEN lod_include{[月份]:SUM([结存数量]):[月份]='5月'}“,得到的结果为(这里称为表A3,由于是内连接,所以3月、4月无法显示):
LOD_2C07D23FC7BBAA | temp_calculation_0 | LOD_C5682C34D69BEC(LOD_TP_2) |
5月 | 5月 | 37120427 |
表A1、表A2、表A3默认进行通过字段‘LOD_2C07D23FC7BBAA‘进行内连接(INNER JOIN),由于表A1中只有’3月‘,表A2中只有’4月‘,表A3中只有’5月‘,内连接的表必然为空表。
四、优化建议
既然表A1、表A2、表A3之间默认是内连接(INNER JOIN),只需要让表A1、表A2、表A3中同时存在“3月“、”4月“、”5月“即可以。也就是把LOD_TM与LOD_TP_0通过条件“ON LOD_TM.`LOD_2C07D23FC7BBAA` = LOD_TP_0.`LOD_2C07D23FC7BBAA` “进行的内连接(INNER JOIN),改为左外连接(LEFT JOIN)。LOD_TP_1、LOD_TP_2同理进行相同操作。具体做法即将上述第43、101、159、261、319、377行代码由INNER JOIN 改为 LEFT JOIN。这样得到的表A1:
LOD_2C07D23FC7BBAA | temp_calculation_0 | LOD_C5682C34D69BEC(LOD_TP_0) |
3月 | 3月 | 35116176 |
4月 | 4月 | NULL |
5月 | 5月 | NULL |
表A2:
LOD_2C07D23FC7BBAA | temp_calculation_0 | LOD_C5682C34D69BEC(LOD_TP_0) |
3月 | 3月 | NULL |
4月 | 4月 | 36286140 |
5月 | 5月 | NULL |
表A3:
LOD_2C07D23FC7BBAA | temp_calculation_0 | LOD_C5682C34D69BEC(LOD_TP_0) |
3月 | 3月 | NULL |
4月 | 4月 | NULL |
5月 | 5月 | 37120427 |
新得到的表A1、表A2、表A3再进行内连接(INNER JOIN)得到的结果是:
LOD_2C07D23FC7BBAA | temp_calculation_0 | LOD_C5682C34D69BEC(LOD_TP_0) |
3月 | 3月 | 35116176 |
4月 | 4月 | 36286140 |
5月 | 5月 | 37120427 |