如何最小化重复查询

时间:2017-05-26 13:37:08

标签: data-analysis qlikview

假设我有两个数据集。 Two tables, one named marriage table showing the combination of two people in marriage, the second table showing details about people. The tables are joined via two foreign keys on PERSON_ID.

在QlikView中,如果我尝试使用如下查询将这些包含在加载中:

sql select marriage_id, primary_person_id, seconary_person_id, marriage_start_date, marriage_end_date from marriage_table;

sql select person_id as primary_person_id, person_id as seconary_person_id, first_name, middle_name, last_name, date_of_birth from person_table;

我会得到一个关于我如何导致自己拥有不准确数据的错误,因为QlikView有两条潜在的路径可以到达PERSON_TABLE。这是有道理的,但我真的很讨厌重复选择和表格的想法,如下所示。

sql select marriage_id, primary_person_id, seconary_person_id, marriage_start_date, marriage_end_date from marriage_table;

sql select person_id as primary_person_id, first_name, middle_name, last_name, date_of_birth from person_table;

sql select person_id as seconary_person_id, first_name, middle_name, last_name, date_of_birth from person_table;

有没有更好的方法可以解决这个问题?

1 个答案:

答案 0 :(得分:0)

您在该图中显示的内容称为"循环引用" (我认为Qlikview称之为" Synthetic Key")这是你应该尽量避免的,因为它可能会使你的应用程序,而不是崩溃,但显示不正确的结果(这是更糟糕的)。 在我看来,你有两个选择:

Op1 - 复制您的PERSON_TABLE,以便PRIMARY_PERSON_ID链接到PERSON_TABLE_1,SECONDARY_PERSON_ID将链接到PERSON_TABLE_2。

PERSON_TABLE_1:
SQL SELECT person_id as primary_person_id,
first_name as first_name_1,
middle_name as middle_name_1,
last_name as last_name_1, 
date_of_birth as date_of_birth_1 
FROM person_table

PERSON_TABLE_2:
SQL SELECT person_id as secondary_person_id,
first_name as first_name_2,
middle_name as middle_name_2,
last_name as last_name_2, 
date_of_birth as date_of_birth_2 
FROM person_table

此选项的问题在于您必须为每个字段选择不同的别名,这通常不太方便,具体取决于您在应用中执行的分析类型。

Op2:创建一个独特的MARRIAGE_TABLE,其中包括两个人的数据。为此,您可以使用两个JOINS创建一个SQL查询(为简单起见,我将仅使用名字和中间名称,但您可以添加所有其他字段)

SELECT T1.*, T2.first_name as first_name_1, T2.middle_name as middle_name_1, 
T3.first_name as first_name_2, T3.middle_name as middle_name_2
FROM MARRIAGE_TABLE AS T1
LEFT JOIN PERSON_TABLE AS T2 ON Q1.primary_person_id = T2.person_id 
LEFT JOIN PERSON_TABLE AS T3 ON Q1.secondary_person_id = T3.person_id 

将生成包含以下列的唯一表:

MARRIAGE_ID PRIMARY_PERSON_ID SECONDARY_PERSON_ID MARRIAGE_START_DATE MARRIAGE_END_DATE FIRST_NAME_1 MIDDLE_NAME_1 FIRST_NAME_2 MIDDLE_NAME_2