限制行,其中ID,日期和代码相同

时间:2018-09-21 17:04:21

标签: sql sql-server-2014

我有一个SQL查询,该查询获取员工的考试数据。在某些情况下,员工(具有相同的EMPLID)具有第二个用工实例,用EMPL_RCD表示,并且他们的数据被保存到每个用工实例的表中。发生这种情况时,我想限制或选择1行(而不是2行),其中EMPLIDEXAM_DTIMMUN_CODE是相同的。

样本数据:

enter image description here

您可以看到上面突出显示的两行是基于这三个字段(EMPLIDEXAM_DTIMMUN_CODE)重复的。在这种情况下,我只想显示其中的一个(可能的情况下,该行带有EMPL_RCD = 0

这是我的查询,产生上面的输出:

SELECT A.EMPLID, A.EMPL_RCD, COMPANY, (CONVERT(CHAR(10),A.EXAM_DT,121)) AS EXAM_DT, A.IMMUN_CODE, F.DESCR,  COUNT(*) AS TOTAL
  FROM (((PS_GHS_HS_IMMUN A 
  LEFT OUTER JOIN  (PS_JOB B INNER JOIN PS_EMPLMT_SRCH_QRY B1 ON (B.EMPLID = B1.EMPLID AND B.EMPL_RCD = B1.EMPL_RCD )) ON  A.EMPLID = B.EMPLID AND B.EMPL_RCD = A.EMPL_RCD ) 
  LEFT OUTER JOIN  PS_LOCATION_TBL E ON  E.LOCATION = B.LOCATION ) 
  LEFT OUTER JOIN  PS_HS_IMMUN_TBL F ON  A.IMMUN_CODE = F.IMMUN_CODE )
  WHERE ( ( B.EFFDT =
        (SELECT MAX(B_ED.EFFDT) FROM PS_JOB B_ED
        WHERE B.EMPLID = B_ED.EMPLID
          AND B.EMPL_RCD = B_ED.EMPL_RCD
          AND B_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))
    AND B.EFFSEQ =
        (SELECT MAX(B_ES.EFFSEQ) FROM PS_JOB B_ES
        WHERE B.EMPLID = B_ES.EMPLID
          AND B.EMPL_RCD = B_ES.EMPL_RCD
          AND B.EFFDT = B_ES.EFFDT)
     AND A.EXAM_DT BETWEEN '09-01-2018' AND GETDATE()
     AND E.EFFDT =
        (SELECT MAX(E_ED.EFFDT) FROM PS_LOCATION_TBL E_ED
        WHERE E.SETID = E_ED.SETID
          AND E.LOCATION = E_ED.LOCATION
          AND E_ED.EFFDT <= B.EFFDT)
     AND F.EFFDT =
        (SELECT MAX(F_ED.EFFDT) FROM PS_HS_IMMUN_TBL F_ED
        WHERE F.SETID = F_ED.SETID
          AND F.IMMUN_CODE = F_ED.IMMUN_CODE
          AND F_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) ))
  GROUP BY  B.COMPANY,  A.EXAM_DT,  A.IMMUN_CODE,  F.DESCR,  E.DESCR, A.EMPLID, A.EMPL_RCD
  ORDER BY A.EXAM_DT

预期输出为:

enter image description here

如果可能的话,我想避免使用CTE,因为我不能在查询引擎中本地使用CTE。

1 个答案:

答案 0 :(得分:0)

似乎您需要MIN(A.EMPL_RCD)

SELECT A.EMPLID, MIN(A.EMPL_RCD), max(B.COMPANY), (CONVERT(CHAR(10),A.EXAM_DT,121)) AS EXAM_DT, A.IMMUN_CODE, F.DESCR,  COUNT(*) AS TOTAL
  FROM (((PS_GHS_HS_IMMUN A 
  LEFT OUTER JOIN  (PS_JOB B INNER JOIN PS_EMPLMT_SRCH_QRY B1 ON (B.EMPLID = B1.EMPLID AND B.EMPL_RCD = B1.EMPL_RCD )) ON  A.EMPLID = B.EMPLID AND B.EMPL_RCD = A.EMPL_RCD ) 
  LEFT OUTER JOIN  PS_LOCATION_TBL E ON  E.LOCATION = B.LOCATION ) 
  LEFT OUTER JOIN  PS_HS_IMMUN_TBL F ON  A.IMMUN_CODE = F.IMMUN_CODE )
  WHERE ( ( B.EFFDT =
        (SELECT MAX(B_ED.EFFDT) FROM PS_JOB B_ED
        WHERE B.EMPLID = B_ED.EMPLID
          AND B.EMPL_RCD = B_ED.EMPL_RCD
          AND B_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10))
    AND B.EFFSEQ =
        (SELECT MAX(B_ES.EFFSEQ) FROM PS_JOB B_ES
        WHERE B.EMPLID = B_ES.EMPLID
          AND B.EMPL_RCD = B_ES.EMPL_RCD
          AND B.EFFDT = B_ES.EFFDT)
     AND A.EXAM_DT BETWEEN '09-01-2018' AND GETDATE()
     AND E.EFFDT =
        (SELECT MAX(E_ED.EFFDT) FROM PS_LOCATION_TBL E_ED
        WHERE E.SETID = E_ED.SETID
          AND E.LOCATION = E_ED.LOCATION
          AND E_ED.EFFDT <= B.EFFDT)
     AND F.EFFDT =
        (SELECT MAX(F_ED.EFFDT) FROM PS_HS_IMMUN_TBL F_ED
        WHERE F.SETID = F_ED.SETID
          AND F.IMMUN_CODE = F_ED.IMMUN_CODE
          AND F_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) ))
  GROUP BY   A.EXAM_DT,  A.IMMUN_CODE,  F.DESCR,  E.DESCR, A.EMPLID