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

Change the relation between two tables to outer join

我有一个 table (table1) 有事实数据。假设 (products, start, end, value1, month[calculated column]) 是列,开始和结束列是时间戳。

我想要的是一个 table 和条形图,它给我每个月的 value1 总和除以每个月的一个因子数(这份报告是年度报告。我的意思是,我将数据加载到 qlik sense 一年)。

我使用开始和结束生成自动日历作为 qlik sense 数据管理器中的时间戳字段。然后,我从开始获取月份,并使用自动日历 (Month(start.autoCalendar.Month)) 的功能将其存储在 table1 中的计算列 "month" 中。

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

的月份兼容的目标 value2

我可以正确计算。但仍然遗漏了一件事。每个月的产品数据都没有值(value1)。例如,假设我有一个产品 (p1,p2...)。我在 table 1 中有数据(6 月、2 月、11 月),p2 中有(Mrz、Apr、Mai、Dec)的数据。因此,当数据以 qlik 形式 table 以及条形图呈现时,我只能看到在事实 table 中具有值的月份。 qlik sense table 包含(2 个维度,即 [products] 和 [month],度量为 m1[sum(value1)/value2])。

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

我想,如果我可以将 qlik sense table 的数据显示为我的关系的右外连接,这可能是一个解决方案 (table1.month>>table2.month).那么,在qlik意义上是否有可能在这样的例子中进行外部连接?或者我的问题有更好的解决方案。

更新

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

// 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

脚本之前:

脚本后:


Qlik Sense(和 Qlikview)中的 table link 更像是完全外部联接。如果你只想显示一个 table(而不是全部)的 id,你可以在你想要的 table 中创建额外的字段,然后在这个字段之上执行你的计算而不是linked 一个。例如:

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

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

在上面的示例中,两个 table 仍将在 id 字段上进行 linked,但如果您只想计算右侧 id 中的值 table (Table2) 你只需要输入 count( MyRightId )

我知道这个问题已经得到解答,我非常喜欢 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;

这会产生以下输出 table:

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