我在2个表中有2条记录,其中 ID 相同,从DB加载时我得到重复值
所以这是我的第一张表:
|--ID--|--Gross Salary--|--Day--|
1 100 1
1 200 2
表2:
|--ID--|--Net Salary--|
1 100
1 200
加载时的结果:
|--ID--|--Gross Salary--|--Net Salary--|--Day--|
1 100 100 1
1 100 200 1
1 200 100 2
1 200 200 2
我需要展示的内容:
|--ID--|--Gross Salary--|--Net Salary--|--Day--|
1 100 100 1
1 200 200 2
我的代码:
Dim loadq As String = "SELECT DriverEmpEarns.LastName, DriverEmpEarns.FirstName, DriverEmpEarns.Mi, DriverEmpEarns.[Position], DriverEmpEarns.[No of Trips 6W], DriverEmpEarns.[Total 6w], DriverEmpEarns.[No of Trips 4W], DriverEmpEarns.[Total 4w], DriverEmpEarns.[SHR], DriverEmpEarns.[PR], DriverEmpEarns.[Additional Incentives], DriverEmpEarns.[SpecialAllowance], DriverEmpEarns.GrossSalary, " & _
"DriverEmpDed.WHTax, DriverEmpDed.PhilHealth, DriverEmpDed.SSS, DriverEmpDed.Vale, DriverEmpDed.Advances, DriverEmpDed.Damages, DriverEmpDed.Pondo, DriverEmpDed.StoreCanteen, DriverEmpDed.Others, DriverEmpDed.NetSalary " & _
"FROM (DriverEmpDed LEFT JOIN DriverEmpEarns ON DriverEmpEarns.EmpPID = DriverEmpDed.EmpPID) " & _
"WHERE [Pday1] >= # " & MetroDateTime1.Value.ToShortDateString & "# AND [Pday2] <= #" & MetroDateTime2.Value.ToShortDateString & "# AND DriverEmpEarns.EmpPID = DriverEmpDed.EmpPID"
答案 0 :(得分:1)
你的table2还需要有一个day
列,然后你将把它包含在你的join子句中。
答案 1 :(得分:0)
它们不是重复的。这是SQL如何工作。您根据EmployeeId要求表加入。由于两个表中存在两次相同的ID,因此获得2x2记录(4)。从其中一个表中删除1条记录,您只获得2条记录。您需要删除第二条记录,设计一条规则,当相同的employeeID存在多次时,该规则控制净工资和工资总额的关系,或者重新设计数据,以便不需要加入。
只需几件事:
SELECT DriverEmpEarns.LastName,
DriverEmpEarns.FirstName,
DriverEmpEarns.Mi,
DriverEmpEarns.[Position],
DriverEmpEarns.[No of Trips 6W],
DriverEmpEarns.[Total 6w],
DriverEmpEarns.[No of Trips 4W],
DriverEmpEarns.[Total 4w],
DriverEmpEarns.[SHR],
DriverEmpEarns.[PR],
DriverEmpEarns.[Additional Incentives],
DriverEmpEarns.[SpecialAllowance],
DriverEmpEarns.GrossSalary,
DriverEmpDed.WHTax,
DriverEmpDed.PhilHealth,
DriverEmpDed.SSS,
DriverEmpDed.Vale,
DriverEmpDed.Advances,
DriverEmpDed.Damages,
DriverEmpDed.Pondo,
DriverEmpDed.StoreCanteen,
DriverEmpDed.Others,
DriverEmpDed.NetSalary
FROM DriverEmpDed
LEFT JOIN DriverEmpEarns
ON DriverEmpEarns.EmpPID = DriverEmpDed.EmpPID
WHERE [Pday1] >= # " & MetroDateTime1.Value.ToShortDateString & "#
AND [Pday2] <= #" & MetroDateTime2.Value.ToShortDateString & "#
AND DriverEmpEarns.EmpPID = DriverEmpDed.EmpPID <--Redundant
AND
DriverEmpEarns.EmpPID = DriverEmpDed.EmpPID"
应该删除