将两个表之间的关系更改为外连接

时间:2016-06-03 09:50:26

标签: qliksense

我有一个表(table1)有事实数据。假设(产品,开始,结束,值1,月[计算列])是列,开始和结束列是时间戳。

我想要的是一个表格和条形图,它给我每个月的值1的总和除以每月的因子数(这个报告是每年的基数。我的意思是,我将数据加载到qlik感觉一年)。

我使用start和end生成autoCalendar作为qlik sense数据管理器中的时间戳字段。然后,我从开始得到月份,并使用autoCalendar(Month(start.autoCalendar.Month))的功能将其存储在table1中的计算列“月”中。

之后,我创建了另一个包含两列(month,value2)的表,value2列是一个因子值,我需要它根据每个月除以value1。这是平均值(sum(value1)/ 1520 [1月],sum(value2)/ 650 [2月])等等。这里的月份和月份列是qlik意义上的关系列。然后我可以在我的表达式中计算总和(value1)并获得与table2的月份兼容的目标值2.

我可以正确地进行计算。但还有一件事是错过的。产品数据在每个月都没有价值(值1)。例如,假设我有一个产品(p1,p2 ......)。我有(1月,2月,11月)的表1中的数据,以及(Mrz,Apr,Mai,Dec)的p2。因此,当数据以qlik意义表和条形图表示时,我只能看到事实表中具有值的月份。 qlik sense表包含(2个维度,即[products]和[month],度量为m1 [sum(value1)/ value2])。

我希望每年有一份报告显示12个月。在我的例子中,我可以看到p1(仅3个月)和p2(4个月)。当没有数据时,度量列[m1] 0和我希望在我的表格和图表中得到0。

我想,如果我可以将qlik sense表的数据显示为我的关系关系的右外连接(table1.month>> table2.month),它可能是一个解决方案。所以,它是否可能在qlik意识到在这样的例子中有外连接?或者有一个更好的解决方案来解决我的问题。

2 个答案:

答案 0 :(得分:2)

<强>更新

知道了。不确定这是否是最佳方法,但在这种情况下,我通常会在脚本加载期间填充缺少的记录。

// Main table
Sales:
Load 
    *,
    ProductId & '-' & Month as Key_Product_Month
;   
Load * Inline [
    ProductId, Month, SalesAmount
    P1       , 1    , 10
    P1       , 2    , 20
    P1       , 3    , 30
    P2       , 1    , 40
    P2       , 2    , 50
];

// Get distinct products and assign 0 as SalesAmount
Products_Temp:
Load 
  distinct ProductId,
  0 as SalesAmount
Resident 
  Sales
;

join (Products_Temp) // Cross join in this case

Load 
  distinct Month
Resident 
  Sales
;

// After the cross join Products_Temp table contains 
// all possible combinations between ProductId and Month 
// and for each combination SalesAmount = 0
Products_Temp_1:
Load 
    *,
    ProductId & '-' & Month as Key_Product_Month1 // Generate the unique id
Resident 
    Products_Temp
;

Drop Table Products_Temp; // we dont need this anymore

Concatenate (Sales)

// Concatenate to main table only the missing ProductId-Month
// combinations that are missing
Load
  *
Resident 
    Products_Temp_1
Where
    Not Exists(Key_Product_Month, Key_Product_Month1)
;

Drop Table Products_Temp_1; // not needed any more
Drop Fields Key_Product_Month1, Key_Product_Month; // not needed any more

在剧本之前:

enter image description here

脚本之后:

enter image description here

Qlik Sense(和Qlikview)中的表链接更像是完全外连接。如果您只想从一个表(而不是全部)显示id,您可以在所需的表中创建其他字段,然后在该字段的顶部而不是链接的字段上执行计算。例如:

Table1:
Load
  id,
  value1
From 
  MyQVD1.qvd (qvd)
;

Table2:
Load
  id,
  id as MyRightId
  value2
From 
  MyQVD2.qvd (qvd)
;

在上面的示例中,两个表仍将链接到id字段,但如果您只想计算右表(id)中的Table2值,则只需输入 count( MyRightId )

答案 1 :(得分:0)

我知道这些问题已得到解答,我非常喜欢Stefan的方法,但希望我的回答可以帮助其他用户。我最近碰到了类似的东西,我使用了与以下脚本略有不同的逻辑:

// Main table
Sales:

Load * Inline [
    ProductId, Month, SalesAmount
    P1       , 1    , 10
    P1       , 2    , 20
    P1       , 3    , 30
    P2       , 1    , 40
    P2       , 2    , 50
];

Cartesian:
//Create a combination of all ProductId and Month and then load the existing data into this table
NoConcatenate Load distinct ProductId Resident Sales;

Join

Load Distinct Month Resident Sales;

Join Load ProductId, Month, SalesAmount Resident Sales; //Existing data loaded

Drop Table Sales;

这会产生以下输出表:

enter image description here

新(最底部)行中的Null值可以保持不变,但如果您更喜欢替换它,请使用Map..Using process

相关问题