从子查询联接中的字段添加日期过滤

时间:2018-04-18 16:26:08

标签: sql sql-server-2014

我有以下查询获取行计数,然后将它们相加以返回单个值(聚合现在完全正常工作):

SELECT

'COUNTS',
SUM(G.ROW_COUNT)  +
SUM(H.ROW_COUNT)  +
SUM(J.ROW_COUNT)  +
SUM(M.ROW_COUNT)  +
SUM(P.ROW_COUNT)  +
SUM(S.ROW_COUNT)  +
SUM(V.ROW_COUNT)  +
SUM(Y.ROW_COUNT) 

FROM
PS_JOB F 
LEFT OUTER JOIN (SELECT EMPLID, EMPL_RCD, COUNT(*) AS ROW_COUNT 
FROM PS_GHS_HS_ANN_EXAM G GROUP BY EMPLID, EMPL_RCD) G
 ON   F.EMPLID = G.EMPLID AND F.EMPL_RCD = G.EMPL_RCD

LEFT OUTER JOIN (SELECT EMPLID, EMPL_RCD, COUNT(*) AS ROW_COUNT FROM      
FROM PS_GHS_HS_ANTINEO H GROUP BY EMPLID, EMPL_RCD) H
 ON   F.EMPLID = H.EMPLID AND F.EMPL_RCD = H.EMPL_RCD

LEFT OUTER JOIN (SELECT EMPLID, EMPL_RCD, COUNT(*) AS ROW_COUNT FROM 
FROM PS_GHS_HS_AUDIO J GROUP BY EMPLID, EMPL_RCD) J
 ON   F.EMPLID = J.EMPLID AND F.EMPL_RCD = J.EMPL_RCD 

LEFT OUTER JOIN (SELECT EMPLID, EMPL_RCD, COUNT(*) AS ROW_COUNT FROM 
FROM PS_GHS_HS_DOT M GROUP BY EMPLID, EMPL_RCD) M
 ON   F.EMPLID = M.EMPLID AND F.EMPL_RCD = M.EMPL_RCD

LEFT OUTER JOIN (SELECT EMPLID, EMPL_RCD, COUNT(*) AS ROW_COUNT 
FROM PS_GHS_HS_HAZMAT P GROUP BY EMPLID, EMPL_RCD) P
 ON   F.EMPLID = P.EMPLID AND F.EMPL_RCD = P.EMPL_RCD

LEFT OUTER JOIN (SELECT EMPLID, EMPL_RCD, COUNT(*) AS ROW_COUNT 
FROM PS_GHS_HS_PREPLACE S GROUP BY EMPLID, EMPL_RCD) S
 ON   F.EMPLID = S.EMPLID AND F.EMPL_RCD = S.EMPL_RCD

LEFT OUTER JOIN (SELECT EMPLID, EMPL_RCD, COUNT(*) AS ROW_COUNT 
FROM PS_GH_RESP_FIT V GROUP BY EMPLID, EMPL_RCD) V
 ON   F.EMPLID = V.EMPLID AND F.EMPL_RCD = V.EMPL_RCD

LEFT OUTER JOIN (SELECT EMPLID, EMPL_RCD, COUNT(*) AS ROW_COUNT 
FROM PS_GHS_HS_ASBESTOS Y GROUP BY EMPLID, EMPL_RCD) Y
 ON   F.EMPLID = Y.EMPLID AND F.EMPL_RCD = Y.EMPL_RCD

WHERE ( ( F.EFFDT = 
 (SELECT MAX(F_ED.EFFDT) FROM PS_JOB F_ED 
  WHERE F.EMPLID = F_ED.EMPLID 
  AND F.EMPL_RCD = F_ED.EMPL_RCD 
  AND F_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) 
AND F.EFFSEQ = 
 (SELECT MAX(F_ES.EFFSEQ) FROM PS_JOB F_ES 
  WHERE F.EMPLID = F_ES.EMPLID 
  AND F.EMPL_RCD = F_ES.EMPL_RCD 
  AND F.EFFDT = F_ES.EFFDT) ))

我希望能够从每个LEFT OUTER JOINed表(G.EXAM_DT,H.EXAM_DT,J.EXAM_DT等等)的日期字段(EXAM_DT)主查询的WHERE子句中添加日期条件。 ),但似乎因为我在JOINS上做一个子查询(为了获得正确的计数聚合),SQL无法识别外(主)查询中的这些字段。有没有办法将子查询中的字段用于主查询中的WHERE子句?

我尝试将Date字段添加为Subquery的一部分(下面的示例):

LEFT OUTER JOIN (SELECT EMPLID, EMPL_RCD, EXAM_DT, COUNT (DISTINCT EMPLID)
AS ROW_COUNT FROM PS_GHS_HS_ANN_EXAM G GROUP BY EMPLID, EMPL_RCD, EXAM_DT) G
 ON  F.EMPLID = G.EMPLID AND F.EMPL_RCD = G.EMPL_RCD

但是当我运行整个查询时它会丢掉计数,所以我试图避免在子查询中改变选择/分组的字段。

为了能够使用日期字段,我有哪些选择?谢谢!

修改

以下是来自以下查询的原始结果(总计)(这些是准确的计数):

enter image description here

或者,以下是每个字段值的总和(总和为40):

enter image description here

以下是我将EXAM_DT添加到每个子查询/连接时的结果(使用我尝试的上述逻辑):

总计:

enter image description here

行计数(总和到48):

enter image description here

所有表格中的示例数据:

PS_GHS_HS_ANN_EXAM(9行):

EMPLID     EMPL_RCD     EXAM_DT     EXAM_TYPE_CD     
 001          0        2018-04-17     ANN
 03           0        2018-04-03     ANN
 4586         0        2018-03-02     ANN
 4586         0        2018-03-26     ANN
 6719         0        2018-03-02     ANN
 2602         0        2018-03-01     ANN
 1351         0        2018-04-16     ANN
 1351         1        2018-04-16     ANN
 4657         0        2018-03-02     ANN

PS_GHS_HS_ANTINEO(5行):

EMPLID     EMPL_RCD     EXAM_DT     EXAM_TYPE_CD 
 9866         0        2018-04-11     ANE
 001          1        2018-04-17     ANE
 03           0        2018-04-03     ANE
 6051         0        2018-03-06     ANE
 2602         0        2018-03-08     ANE

PS_GHS_HS_AUDIO(5行):

 EMPLID     EMPL_RCD     EXAM_DT     EXAM_TYPE_CD 
  001         0         2018-03-28     AUD
  001         1         2018-04-17     AUD
  01          0         2018-03-14     AUD
  03          0         2018-04-03     AUD
  4586        0         2018-03-29     AUD

PS_GHS_HS_DOT(3行):

EMPLID     EMPL_RCD     EXAM_DT     EXAM_TYPE_CD
 001          1        2018-04-17      DOT
 5829         0        2018-04-03      DOT
 2602         0        2018-03-08      DOT

PS_GHS_HS_HAZMAT(4行):

EMPLID     EMPL_RCD     EXAM_DT     EXAM_TYPE_CD
 001          1        2018-04-17      HAZ
 001          1        2018-04-18      HAZ
 001          1        2018-05-14      HAZ
 2602         0        2018-03-01      HAZ

PS_GHS_HS_PREPLACE(9行):

EMPLID     EMPL_RCD     EXAM_DT     EXAM_TYPE_CD
 01           0        2018-03-20      PRE
 03           0        2018-04-02      PRE
 04           0        2018-04-04      PRE
 05           0        2018-04-03      PRE
 07           0        2018-04-04      PRE
 0285         0        2018-04-09      PRE
 1325         0        2018-04-09      PRE
 5272         0        2018-04-09      PRE
 4555         0        2018-04-06      PRE

PS_GHS_HS_RESP_FIT(4行):

EMPLID     EMPL_RCD     EXAM_DT     EXAM_TYPE_CD
 4299         0        2018-04-04      RSP
 3003         0        2018-04-02      RSP
 8908         0        2018-04-11      RSP
 2602         0        2018-04-04      RSP

PS_GHS_HS_ASBESTOS(1行):

EMPLID     EMPL_RCD     EXAM_DT     EXAM_TYPE_CD
 2602         0        2018-03-01      ASB

上表中的所有计数总共为40,这就是我在原始查询中得到的结果。但是,在每个子查询中添加日期选择时,总计不正确。

编辑2:

PS_JOB(已过滤适用的EMPLID):

EMPLID      EMPL_RCD      EFFDT      EFFSEQ      PER_ORG
00000044       0       2018-04-05      0          CWR
001            0       2018-01-01      0          CWR
001            1       2018-03-16      0          EMP
01             0       2018-03-14      0          CWR
03             0       2018-02-26      0          CWR
03             1       2018-03-23      0          CWR
04             0       2018-03-21      0          CWR
04             0       2018-03-22      0          CWR
04             0       2018-03-22      1          CWR
05             0       2018-03-20      0          CWR
05             0       2018-03-22      0          CWR
55829          0       1987-08-10      0          EMP
07             0       2018-03-30      1          CWR
4299           0       2018-01-01      0          EMP
0285           0       2017-07-01      0          EMP
1325           0       2018-01-01      0          EMP
5272           0       2018-01-01      0          EMP
6051           0       2018-01-01      0          EMP
3003           0       2018-01-01      0          EMP
4555           0       2018-01-01      0          EMP
8908           0       2018-01-01      0          EMP
2602           0       2018-01-01      0          EMP
1351           0       2018-01-01      0          EMP
1351           1       2018-03-22      0          CWR
4657           0       2008-10-18      0          EMP

1 个答案:

答案 0 :(得分:0)

这里你应该这样做。删除#符号并注意拼写(一个表名称错误)

select count(b.emplid)+
  count(c.emplid)+
  count(d.emplid)+
  count(e.emplid)+ 
  count(ff.emplid)+ 
  count(g.emplid)+ 
  count(h.emplid)+ 
  count(i.emplid)
   from #PS_JOB f 
  left join #PS_GHS_HS_ANN_EXAM b on f.EMPLID=b.EMPLID and f.EMPL_RCD=b.EMPL_RCD
    left join #PS_GHS_HS_ANTINEO c on f.EMPLID=c.EMPLID and f.EMPL_RCD=c.EMPL_RCD
      left join #PS_GHS_HS_AUDIO  d on f.EMPLID=d.EMPLID and f.EMPL_RCD=d.EMPL_RCD
      left join #PS_GHS_HS_DOT  e on f.EMPLID=e.EMPLID and f.EMPL_RCD=e.EMPL_RCD
        left join #PS_GHS_HS_HAZMAT ff on f.EMPLID=ff.EMPLID and f.EMPL_RCD=ff.EMPL_RCD
        left join #PS_GHS_HS_PREPLACE g on f.EMPLID=g.EMPLID and f.EMPL_RCD=g.EMPL_RCD
           left join #PS_GHS_HS_RESP_FIT h on f.EMPLID=h.EMPLID and f.EMPL_RCD=h.EMPL_RCD
        left join #PS_GHS_HS_ASBESTOS i on f.EMPLID=i.EMPLID and f.EMPL_RCD=i.EMPL_RCD
        WHERE ( ( f.EFFDT = 
 (SELECT MAX(F_ED.EFFDT) FROM #PS_JOB F_ED 
  WHERE F.EMPLID = F_ED.EMPLID 
  AND F.EMPL_RCD = F_ED.EMPL_RCD 
  AND F_ED.EFFDT <= SUBSTRING(CONVERT(CHAR,GETDATE(),121), 1, 10)) 
AND f.EFFSEQ = 
 (SELECT MAX(F_ES.EFFSEQ) FROM #PS_JOB F_ES 
  WHERE F.EMPLID = F_ES.EMPLID 
  AND F.EMPL_RCD = F_ES.EMPL_RCD 
  AND F.EFFDT = F_ES.EFFDT) ))
***and [ADD DATE FILTERS HERE]***