[英]sql SELECT query - which merges 2 identical tables?
So I have 2 tables. 所以我有2张桌子。 Employee
and Draft_Employee
. Employee
和Draft_Employee
。 They are identical, except that properties in Draft_Employee
allow nulls. 它们是相同的,除了Draft_Employee
中的属性允许为空。
The reason for the 2 identical tables, is that the draft_Employee is a table used for an import procedure - it is a tempoary container. 使用2个相同表的原因是,draft_Employee是用于导入过程的表-它是临时容器。 We don't want the data messed up with the production employees. 我们不希望数据与生产人员混淆。
Now, when a employee has to be imported, the system has to check if the employee already exists in the database. 现在,当必须导入雇员时,系统必须检查该雇员是否已存在于数据库中。
First it see's if it can find an employee in Employee table with the same EmpID. 首先,它是否可以在Employee表中找到具有相同EmpID的雇员。 If found, it will look at employee in Draft_Employee and find the properties which are NULL or EMPTY. 如果找到,它将在Draft_Employee中查看employee并找到NULL或EMPTY属性。 It will then take the value for the same field in Employee table and put it into the empty or NULL fields in draft_Employee 然后它将采用Employee表中相同字段的值,并将其放入draft_Employee中的空字段或NULL字段中
empID name something1 something2 | empID name something1 something2
----- ---- ---------- ---------- | ---- ---- ---------- -----------
1 Casper blahblah blahblah2 | 2 Michael NULL text2fs
2 Michael txttxt |
Right is Employee and left is Draft_Employee. 右边是Employee,左边是Draft_Employee。
I want an sql query that produces 我想要一个产生的SQL查询
empID name something1 something2
----- ---- ---------- ----------
2 Michael txttxt text2fs
The closest I have come, is with LEFT OUTER JOIN
but it gives me data from both tables 我最接近的是LEFT OUTER JOIN
但它为我提供了来自两个表的数据
EDIT: My query. 编辑:我的查询。 I did not use it before, because the spelling is danish. 我以前没用过它,因为拼写是丹麦语。
SELECT * FROM Kladde_Ressource
LEFT OUTER JOIN Ressource
ON Ressource.RessourceID = Kladde_Ressource.RessourceID
WHERE Kladde_Ressource.EAN = ''
OR Kladde_Ressource.navnLang = ''
OR Kladde_Ressource.navnKort = ''
etc...
I don't entirely grasp your requirement so I am basing my query on the data and required result you've posted. 我并不完全了解您的要求,因此我将根据您发布的数据和所需结果进行查询。
As to get the results merged into one result set 为了将结果合并到一个结果集中
Statement 声明
SELECT e.empID
, COALESCE(e.name, de.name) AS name
, COALESCE(e.something1, de.something1) AS something1
, COALESCE(e.something2, de.something2) AS something2
FROM employee e
INNER JOIN draft_employee de ON de.empID = e.empID
This should do the trick: 这应该可以解决问题:
SELECT Employee.empId,
ISNULL(Employee.name, Draft_Employee.name) AS name,
ISNULL(Employee.something1, Draft_Employee.something1) AS something1,
ISNULL(Employee.something2, Draft_Employee.something2) AS something2
FROM Employee LEFT OUTER JOIN
Draft_Employee ON Employee.empID = Draft_Employee.EmpId
Use the coalesce
function to get the first non-null value: 使用coalesce
函数获取第一个非空值:
select
e.empID,
coalesce(d.name, e.name),
coalesce(d.something1, e.something1),
coalesce(d.something2, e.something2)
from
Employee e
inner join Draft_Employee d on d.empID = e.empID
How about using UNION
and GROUP BY
: 如何使用UNION
和GROUP BY
:
SELECT empID, name, MAX(something1) AS something1, MAX(something2) AS something2
FROM (
SELECT empID, name, something1, something2 FROM Employee WHERE empID = 2
UNION
SELECT empID, name, something1, something2 FROM draft_Employee WHERE empID = 2
)
GROUP BY empID, name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.