SQL 如何连接两个具有不同列的查询并连接

SQL How to join two query's with different columns and joins

我有 2 个查询都使用连接,select 来自多个表的各个列产生以下结果集:

companyID   workType    contractnumber  employeenumber  Value2
1           1C          9999999         111111          2547.21
1           1C          9999999         222222          863.67
1           1C          9999999         333333          2962.15
1           1C          9999999         444444          1971.61
1           1C          9999999         555555          152.41
1           1C          9999999         666666          155.90
1           1C          9999999         777777          657.20


companyID   normalWorkType  employeeNumber  value1
1           1C              11111           1016.08
1           1C              22222           3118.05
1           1C              33333           2628.81
1           61              44444           2547.21

我希望加入这些以产生以下结果..任何人都可以解释正确的语法是什么吗?

companyID   normalWorkType  contractnumber  employeeNumber  value1  Value2
1           1C              9999999         11111           1016.08 2547.21
1           1C              9999999         22222           3118.05 863.67
1           1C              9999999         33333           2628.81 2962.15
1           61              9999999         44444           2547.21 1971.61
1           1C              9999999         55555           0       152.41
1           1C              9999999         66666           0       155.90
1           1C              9999999         77777           0       657.20

上面的分组是在 companyID 和 employeeNumber

尝试下面的查询

select Table1.companyId,Table2.normalworktype,Table1.contractnumber,
Table1.employeenumber,Table2.value1,Table1.value2 
from Table1 
inner join Table2 on Table1.companyid = Table2.companyid 
      and Table1.employeeNumber=Table2.employeeNumber

尝试左外连接:

SELECT t1.companyID,
       t2.normalWorkType,
       t1.contractnumber,
       t1.employeeNumber,
       coalesce(t2.value1,0) as t2.value1,
       t1.value2
FROM DatabaseName.t1 AS t1 LEFT OUTER JOIN
     DatabaseName.t2 AS t2 ON t1.employeenumber = t2.employeenumber  
       AND t1.companyID = t2.companyID

您可以将您的搜索结果放入 2 个临时 table,然后加入这个 2 个临时 table,或者为您的每个 select 使用联盟,然后加入他们。 喜欢:

SELECT 
  *
FROM (
  SELECT 
     companyID
    ,workType
    ,contractnumber
    ,employeenumber
    ,Value2
  FROM table1
  ) t1
INNER JOIN (
  SELECT 
     companyID
    ,normalWorkType
    ,contractnumber
    ,employeeNumber
    ,value1
    ,Value2
  FROM table2
  ) t2 
    ON t1.companyID = t2.companyid

或者将你的结果放入 2 个不同的温度 table 然后加入它们

SELECT 
   companyID
  ,workType
  ,contractnumber
  ,employeenumber
  ,Value2
INTO #tt1
FROM table1

SELECT 
   companyID
  ,normalWorkType
  ,contractnumber
  ,employeeNumber
  ,value1
  ,Value2
INTO #tt2
FROM table2

SELECT *
FROM #tt1 t1
INNER JOIN #tt2 t2 ON t1.companyid = t2.companyID

我在这里假设建议的结果集中的合同编号实际上是您第一次查询的合同编号,不同之处只是一个拼写错误。如果那是真的,这将让你得到你想要的。

SELECT
  q1.companyID   
  COALESCE(q2.normalWorkType,q1.workType) AS normalWorkType
  q1.contractnumber  
  q1.employeeNumber  
  COALESCE(q2.value1  ,0) as value1
  q1.Value2
FROM
  (
    <Your first query>
  ) as q1
LEFT JOIN
  (
    <Your second query>
  ) as q2
    ON
      q1.companyID = q2.companyID
      AND
      q1.employeeNumber = q2.employeeNumber;

感谢大家的回复,

在尝试了 Husam Ebish 的建议后,这并不是我所需要的,但引导我进入了这个话题。 (对你的回答投了赞成票)

How to combine two query results into one and I have different column name

我发现真正有用的是下面链接线程中突出显示的这些信息。在尝试了不同的 "left" 连接后,建议突出显示的评论引导我尝试 "full outer join",这给了我正确的结果集。

If you only have one row from each query, it's just a CROSS JOIN

SELECT
  *
FROM
  (query1)   AS q1
CROSS JOIN
  (query2)   AS q2

If you have more than one row from each query, you need an INNER JOIN or maybe a FULL OUTER JOIN and some relationship between the two sets of data

SELECT
  *
FROM
  (query1)   AS q1
FULL OUTER JOIN
  (query2)   AS q2
      ON  q2.id2 = q1.id1

注:

  • UNION 垂直追加数据
  • JOIN 水平追加数据
SELECT
  *
FROM
  (select top 1  VL1,VL2,VL3 from current_voltage AS q1
where deviceimei ='233'
order by devicetimestamp)   AS q1
CROSS JOIN
  (select top 1  OTI,WTI,ATI from overview  AS q2
where deviceimei ='233'
order by devicetimestamp  )   AS q2