如何从查询中的另一个表中获取记录?

时间:2018-07-11 12:57:26

标签: sql sql-server

我有两个表tblEmployeetblTarget。 这是我的SQL查询:

select EmpCode,E.TM_Name, CurrentTarget, CreatedDate,RankOrder,A.PreviousTarget from(
select EmpCode, CurrentTarget, CreatedDate,RankOrder,T.PreviousTarget from (
    Select
        EmpCode, CurrentTarget, CreatedDate,PreviousTarget,
        Rank() over (partition by EmpCode order by CreatedDate desc) RankOrder
    From tbl_Target
) T WHERE RankOrder=1)A
left join  tblEmployee E on A.EmpCode=E.TM_Emp_Id   

这就是我所得到的,而且很完美。

EmpCode        TM_Name         CurrentTarget CreatedDate             RankOrder     PreviousTarget
-------------- --------------- ------------- ----------------------- ------------- --------------
20078          kaul            60            2018-07-11 05:05:03.687 1             50
56042          Bob             40            2018-07-11 05:05:03.687 1             20
56079          Alice           30            2018-07-11 05:05:03.703 1             30
56080          Sikon           50            2018-07-11 05:05:03.703 1             40
56094          Ssohy           35            2018-07-11 05:05:03.703 1             20

我只得到tblTarget中存在的那些Employee结果。没有目标的员工没有显示。 虽然我希望它们显示为0 CurrentTarget和0 PreviousTarget。 我想要这样的输出:

EmpCode        TM_Name         CurrentTarget CreatedDate             RankOrder     PreviousTarget
-------------- --------------- ------------- ----------------------- ------------- --------------
20078          kaul            60            2018-07-11 05:05:03.687 1             50
56042          Bob             40            2018-07-11 05:05:03.687 1             20
56079          Alice           30            2018-07-11 05:05:03.703 1             30
56080          Sikon           50            2018-07-11 05:05:03.703 1             40
56094          Ssohy           35            2018-07-11 05:05:03.703 1             20
101            Joe             0             2018-08-12 05:05:03.687 1             0
102            John            0             2018-08-12 05:05:03.687 1             0
103            Sid             0             2018-08-12 05:05:03.687 1             0
104            Manous          0             2018-08-12 05:05:03.687 1             0

我该怎么做?

3 个答案:

答案 0 :(得分:1)

您需要更改left-join的方向。您需要所有员工,甚至在目标中没有匹配行的员工,因此您可以切换表的顺序并使Employee表成为左表(我在下面的代码中做了)或您将联接更改为right join

select EmpCode, e.TM_Name, isnull(CurrentTarget, 0) CurrentTarget, CreatedDate, RankOrder, isnull(A.PreviousTarget, 0) PreviousTarget
from tblEmployee e 
               left join 
                         (
                             select EmpCode, CurrentTarget, CreatedDate,RankOrder,T.PreviousTarget 
                             from (
                                      Select EmpCode, CurrentTarget, CreatedDate,PreviousTarget, Rank() over (partition by EmpCode order by CreatedDate desc) RankOrder
                                      From tbl_Target
                                  ) T WHERE RankOrder=1
                         ) A on A.empCode = e.tm_emp_id

在显示CurrentTarget和PreviousTarget时,由于对于没有目标的员工,它们将是NULL,因此您可以使用ISNULL()函数并返回0。

答案 1 :(得分:1)

您需要tblEmployee中的所有记录,并且只需要tblTarget中匹配的记录...

除了每位没有匹配目标的员工外,A.PreviousTarget将是NULL,因此您需要使用ISNULL函数对其进行管理。

因此,您只需将查询中的Left Join更改为Right Join,或将其重写如下:

SELECT 
    EmpCode,
    E.TM_Name, 
    CurrentTarget, 
    CreatedDate,RankOrder,
    ISNULL(A.PreviousTarget,0) AS PreviousTarget -- <==== ATTENTION
FROM  tblEmployee AS E LEFT JOIN -- <==== ATTENTION
(
   SELECT 
       EmpCode, 
       CurrentTarget, 
       CreatedDate,
       RankOrder,
       T.PreviousTarget 
   FROM (
          SELECT
              EmpCode, 
              CurrentTarget, 
              CreatedDate,
              PreviousTarget,
              Rank() OVER (PARTITION BY EmpCode ORDER BY CreatedDate DESC) AS RankOrder
           FROM tbl_Target
         )AS T 
    WHERE RankOrder=1
)AS A
ON A.EmpCode=E.TM_Emp_Id  

答案 2 :(得分:1)

虽然在没有记录存在的情况下,我看不到CreatedDate和RankOrder取自何处,但对于此类记录,CurrentTarget和PreviousTarget会显示0:

WITH
  T (EmpCode, CurrentTarget, CreatedDate, PreviousTarget, RankOrder) AS (
    SELECT EmpCode, CurrentTarget, CreatedDate, PreviousTarget
      , RANK() OVER (PARTITION BY EmpCode ORDER BY CreatedDate DESC)
    FROM tbl_Target
  )
SELECT 
  E.TM_Emp_Id,
  E.TM_Name,
  ISNULL(T.CurrentTarget, 0),
  T.CreatedDate,
  T.RankOrder,
  ISNULL(T.PreviousTarget, 0)
FROM tblEmployee E 
  LEFT JOIN T ON E.TM_Emp_Id = T.EmpCode AND T.RankOrder = 1