Qlikview - 加入两个 table

Qlikview - join two table

我需要在 Qlikview 中加入两个 table 才能得到结果。

Table:

我需要加入这两个 table 才能得到这样的结果 table

有什么想法吗?我可以使用交叉 table 吗?如何使用?

有几种方法可以做到这一点。

  1. 使用关联。加载 Table 1 两次并连接,创建一个复合键。所以你最终会得到字段 ReasonLocation 和 Quantity。然后加载 Table 2 创建相同的组合键,为您提供 ReasonLocation、Location、Reason & Answer。然后 table 将关联到该组合键。
  2. 使用联接。加载 Table1,左加入 Table 1 基于 Reason 和 if 语句,如 if [Location] = 'LocA' then [LocA] else [LocB]。这可能需要您先将其加载到临时 table 中,然后在常驻加载中执行 if 语句。

您也可以结合两者并根据 ReasonLocation 字段加入 #1 中的 table。

希望对您有所帮助 - 抱歉,它没有完全解决...

对于 Table1,您可以使用 CrossTable 功能来 "rotate" table,但保留第一列。

例如:

CrossTable(Location, Quantity)
Load 
  Reason, 
  LocA, 
  LocB
From 
  [Data.xlsx] (ooxml, embedded labels, table is Table1)
;

这之后的结果table将是:

Location    Reason  Quantity
LocA        R1      5
LocA        R2      4
LocA        R3      5
LocA        R4      3
LocB        R1      2
LocB        R2      2
LocB        R3      3
LocB        R4      5

(您可以在 Qlik 的帮助站点了解更多关于 CrossTable 的信息 - CrossTable

在使用这种格式的 Table1 之后,您可以创建 composite key(如 x3ja 所建议的)。 Composite key 基本上是两个(或更多)字段连接在一起。在您的情况下,table 之间的连接应该在两个字段上 - LocationReason

// CrossTable the data to get it in correct format
Table1_Temp:
CrossTable(Location, Quantity)
Load 
  Reason, 
  LocA, 
  LocB
From 
  [Data.xlsx] (ooxml, embedded labels, table is Table1)
;

// Resident load to form the composite key
// based on Location and Reason fields
Table1:
Load
  Location & '|' & Reason as Key,
  Quantity
Resident
  Table1_Temp
;

// We dont need Table1_Temp table anymore
Drop Table Table1_Temp;

//Load the second table and create the same composite key
Table2:
Load 
  Location & '|' & Reason as Key,
  Location, 
  Reason, 
  Answer
From 
  [Data.xlsx] (ooxml, embedded labels, table is Table2)
;

重新加载后,您的数据模型将如下所示:

以及数据:

请注意,AnswerLocationReason 的值在底部两行中为 null。这是因为 Table2 中的数据(根据您的屏幕截图)不包含 LocB and R2LocA and R4 的组合,但 Table1 包含。

如果您只想保留两个 table 中都存在的组合,则方法类似但有两个区别:

  • Table2 应首先加载
  • 使用keep函数排除不常见的记录在Table1
  • 加载

(keep 在 Qlik 的帮助站点 - keep)

如果您想查看脚本的运行情况,只需在 example qvw

中注释第一个选项卡并取消注释第二个选项卡