我使用此查询尝试拉取员工的每个依赖项。但无论我如何加入这些表,查询都会重复每个员工两到三次。因此,例如,如果我有一个孩子和一个配偶依赖,当我运行查询时,我会为我的孩子获得两行,为我的配偶获得两行。我能做些什么才能让每个依赖一次而不重复它们?我试图通过EmpID对它们进行分组,但是没有成功。
SELECT
dbo.DBdependent.EmpTableID,
dbo.DBdependent.Relation,
dbo.DBdependent.SSN AS DepSSN,
dbo.DBdependent.DOB,
dbo.DBdependent.Name,
dbo.DBEmployee.EmpID,
dbo.DBEmployee.FName,
dbo.DBEmployee.LName,
dbo.DBEmployee.SSN,
dbo.DBdepElections.HLTH,
dbo.DBdepElections.DNTL,
dbo.DBdepElections.VSN,
dbo.DBdepElections.LINS,
dbo.DBdepElections.LGLPLN,
dbo.DBdepElections.IDSHLD,
dbo.DBdepElections.ACCDNT,
dbo.DBdepElections.CNCR,
dbo.DBdepElections.HPIND,
dbo.DBdepElections.CRTCIL
FROM
dbo.DBdependent
LEFT OUTER JOIN
dbo.DBdepElections ON dbo.DBdependent.EmpTableID = dbo.DBdepElections.EmpTableID
RIGHT OUTER JOIN
dbo.DBEmployee ON dbo.DBdependent.EmpTableID = dbo.DBEmployee.ID
答案 0 :(得分:0)
正如评论中所述,很难根据你的问题判断,但尝试这样的事情:
SELECT
d.EmpTableID,
d.Relation,
d.SSN AS DepSSN,
d.DOB,
d.Name,
e.EmpID,
e.FName,
e.LName,
e.SSN,
el.HLTH,
el.DNTL,
el.VSN,
el.LINS,
el.LGLPLN,
el.IDSHLD,
el.ACCDNT,
el.CNCR,
el.HPIND,
el.CRTCIL
FROM
DBEmployee e
LEFT JOIN
DBdependent d ON d.EmpTableID = e.ID
LEFT JOIN
dbo.DBdepElections el ON d.EmpTableID = el.EmpTableID