【问题标题】:Change the relation between two tables to outer join将两个表之间的关系更改为外连接
【发布时间】:2016-10-03 07:54:18
【问题描述】:

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

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

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

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

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

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

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

【问题讨论】:

    标签: qliksense


    【解决方案1】:

    更新

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

    // 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)中的表格链接更像是完全外连接。如果您只想从一个表(而不是全部)中显示id,您可以在您想要的表中创建其他字段,然后在该字段的顶部而不是链接的字段上执行计算。例如:

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

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

    【讨论】:

    • 嗨 Stefan,实际上,我的问题是关于其他的。
    • 你能再解释一下吗?可以举个例子吗?
    • 对不起,我在完成评论之前错误地按下了(Enter)按钮。我一直在写细节。我将编辑问题,因为评论字符有限,我无法在评论中发布详细信息。
    • Stefan,我已经完成了问题的编辑。现在问题更清楚了吗?
    【解决方案2】:

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

    这会产生以下输出表:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多