我很难将多个表连接在一起;这样做时,我得到了意外的重复数据。
基本上,每个应用程序都可以具有一个或多个表单。每个申请人可以具有一个或多个表格。一个申请人可以拥有一个或多个地址;但是,为简单起见,我只想返回一个地址。
SELECT
APPLICANT.FIRST_NAME,
APPLICANT.LAST_NAME,
ADDRESS.STREET,
ADDRESS.CITY,
ADDRESS.STATE,
ADDRESS.POSTAL_CODE,
FORM.SERIAL
FROM
APPLICATION JOIN
FORM ON
FORM.APPLICATION_SERIAL = APPLICATION.SERIAL JOIN
APPLICANT ON
FORM.APPLICATION_SERIAL = APPLICANT.APPLICATION_SERIAL JOIN
APPLICANT_ADDRESS ON
APPLICANT.SERIAL = APPLICANT_ADDRESS.APPLICANT_SERIAL
运行此查询时,如果一个应用程序有两个申请人,每个申请人都有自己的地址,每个人都有一个表格,则每个申请人的结果重复两次。我尝试选择DISTINCT
列以及按序列号,姓氏,名字等分组,但无济于事。
+------------+-----------+-----------------+--------------+-------+-------------+-------------+
| First_Name | Last_Name | Street | City | State | Postal_code | Form_Serial |
+------------+-----------+-----------------+--------------+-------+-------------+-------------+
| John | Doe | 123 Main Street | Magical City | ZZ | 00000 | 001 |
| John | Doe | 123 Main Street | Magical City | ZZ | 00000 | 001 |
| Jane | Doe | 456 Y Street | Another City | ZZ | 00000 | 002 |
| Jane | Doe | 456 Y Street | Another City | ZZ | 00000 | 002 |
+------------+-----------+-----------------+--------------+-------+-------------+-------------+
将这些表连接在一起时,防止重复行的最佳方法是什么?
答案 0 :(得分:1)
在您的模式中,语句“每个申请人可以有一个或多个表单”不会遵循-Form
实体与Application
实体相关,但与Applicant
不相关一。
拥有重复的样本数据将是很好的。
WITH
APPLICATION (Serial) as (values
(1)
)
, FORM (Serial, Application_serial) as (values
('001', 1)
--, ('002', 1)
)
, APPLICANT (Serial, Application_Serial, First_Name, Last_Name) as (values
(1, 1, 'John', 'Doe')
, (2, 1, 'Jane', 'Doe')
)
, ADDRESS (Serial, Applicant_Serial, Street, City, State, Postal_code) as (values
(1, 1, '123 Main Street', 'Magical City', 'ZZ', '00000')
, (2, 2, '456 Y Street', 'Another City', 'ZZ', '00000')
)
SELECT
APPLICANT.FIRST_NAME, APPLICANT.LAST_NAME
, ADDRESS.STREET, ADDRESS.CITY, ADDRESS.STATE, ADDRESS.POSTAL_CODE
, FORM.SERIAL AS FORM_SERIAL
FROM APPLICATION
JOIN FORM ON FORM.APPLICATION_SERIAL = APPLICATION.SERIAL
JOIN APPLICANT ON FORM.APPLICATION_SERIAL = APPLICANT.APPLICATION_SERIAL
JOIN ADDRESS ON APPLICANT.SERIAL = ADDRESS.APPLICANT_SERIAL;
FIRST_NAME LAST_NAME STREET CITY STATE POSTAL_CODE FORM_SERIAL
---------- --------- --------------- ------------ ----- ----------- -----------
John Doe 123 Main Street Magical City ZZ 00000 001
Jane Doe 456 Y Street Another City ZZ 00000 001
2 record(s) selected.
我的示例不返回重复项。
但是,如果您取消注释此Form
的第二个Application
(而不是Applicant
),则会得到结果。也就是说,第二个Form
记录是您在输出中得到重复项的原因。
对于给定的FORM_SERIAL
,如果有多个Forms
,则应决定在Application
列中返回什么。