将用户表连接到两个不同的任务表?

时间:2016-06-01 18:23:38

标签: sql sql-server join inner-join

我在为我们的业务构建的此查询中有一个关于加入用户表的问题。它是基于用户完成的7个不同任务的小生产率报告。

有两个任务表: 1)核对表任务 2)请求的任务

请求的任务是Checklist Task的扩展。 请求的任务具有REQUESTED DATE列,该列位于请求的任务表中 请求的任务具有COMPLETED DATE列,该列位于Checklist Task表中。

当我运行下面的查询时,显然是SecurityIdentity表,它仅由CompletedByID通过Checklist Task表连接以显示其全名。

任务名称'录制的文档'这是一个请求的核对表,具有请求日期(RequestedDate来自请求任务表)和接收日期(来自核对表表的CompletedDate),来自上面提到的两个不同的表。

SELECT o.number   AS 'OrderNo',
       s.fullname AS 'CPC Emp',
       Max(CASE
             WHEN t.description = 'Loan Package to Lender' THEN
             Dateadd(hour, -4, t.completeddate)
             ELSE NULL
           END)   AS 'Loan Pck To Lender',
       Max(CASE
             WHEN t.description = 'Recording Audit' THEN
             Dateadd(hour, -4, t.completeddate)
             ELSE NULL
           END)   AS 'Recording Audit',
       Max(CASE
             WHEN t.description = 'Recorded Docs' THEN
             Dateadd(hour, -4, rt.requesteddate)
             ELSE NULL
           END)   AS 'Recorded Docs Requested',
       Max(CASE
             WHEN t.description = 'Recorded Docs' THEN
             Dateadd(hour, -4, t.completeddate)
             ELSE NULL
           END)   AS 'Recorded Docs Received',
       Max(CASE
             WHEN t.description = 'Recorded Docs to Lender' THEN
             Dateadd(hour, -4, t.completeddate)
             ELSE NULL
           END)   AS 'Recorded Docs to Lender',
       Max(CASE
             WHEN t.description = 'Recorded Docs to Purchaser' THEN
             Dateadd(hour, -4, t.completeddate)
             ELSE NULL
           END)   AS 'Recorded Docs to Purchaser',
       Max(CASE
             WHEN t.description = 'Title Policy to Lender' THEN
             Dateadd(hour, -4, t.completeddate)
             ELSE NULL
           END)   AS 'TP to Lender',
       Max(CASE
             WHEN t.description = 'Title Policy to Purchaser' THEN
             Dateadd(hour, -4, t.completeddate)
             ELSE NULL
           END)   AS 'TP Purchaser'
FROM   pf.orderinfo oi
       INNER JOIN pfm.[order] o
               ON ( o.rootid# = oi.rootid )
       INNER JOIN core.profile op
               ON ( oi.owningprofileid = op.id )
       INNER JOIN zref.orderstatus os
               ON ( oi.orderstatus = os.id )
       INNER JOIN zref.producttype pt
               ON ( o.producttypeid = pt.id
                    AND pt.id <> '15' )
       INNER JOIN pfm.task t
               ON ( t.rootid# = oi.rootid
                    AND ( t.description IN ( 'Loan Package to Lender',
                                             'Recording Audit',
                                                 'Recorded Docs to Lender',
                                             'Recorded Docs to Purchaser',
                                             'Title Policy to Lender',
                                             'Title Policy to Purchaser'
                                             ,
                                                   'Recorded Docs' ) ) )
       LEFT OUTER JOIN pfm.requestedtask rt
                    ON ( rt.rootid# = t.rootid#
                         AND rt.id# = t.id#
                         AND rt.lastid# = t.lastid# )
       LEFT OUTER JOIN core.securityidentity s
                    ON ( s.id = t.completedbyid )
WHERE  ( op.NAME LIKE 'BH104%' -- WNCW Res Profiles Begin.
          OR op.NAME LIKE 'CL170%'
          OR op.NAME LIKE 'CM104%'
          OR op.NAME LIKE 'IN103%'
          OR op.NAME LIKE 'SF107%'
          OR op.NAME LIKE 'AW170%'
          OR op.NAME LIKE 'JW170%'
          OR op.NAME LIKE 'KH170%'
          OR op.NAME LIKE 'PM170%'
          OR op.NAME LIKE 'SC170%'
          OR op.NAME LIKE 'EC102%'
          OR op.NAME LIKE 'MT175%'
          OR op.NAME LIKE 'CU108%'
          OR op.NAME LIKE 'NF106%'
          OR op.NAME LIKE 'GA105%'
          OR op.NAME LIKE 'SL105%'
          OR op.NAME LIKE 'PC135%'
          OR op.NAME LIKE 'PE103%'
          OR op.NAME LIKE 'OC130%'
          OR op.NAME LIKE 'WF130%'
          OR op.NAME LIKE 'SS100%'
          OR op.NAME LIKE 'SO191%'
          OR op.NAME LIKE 'CA115%'
          OR op.NAME LIKE 'DO115%'
          OR op.NAME LIKE 'WC115%'
          OR op.NAME LIKE 'WO115%' --WNCW RES Profiles End.
             AND op.enabled = 1
             AND oi.istemplate = 0
             AND oi.number NOT LIKE '%test%'
             AND oi.number NOT LIKE 'tr%' )
       AND oi.number = 'WC115-13-0048'
       AND s.fullname IS NOT NULL
GROUP  BY s.fullname,
          o.number
ORDER  BY 2 DESC  

当我查询这个sql时,结果是红色方块: Result

然而,斯泰西没有要求,但凯西做了。因此,结果日期/时间戳应该显示为Kathy请求Recorded Docs而不是Stacey。

我已经尝试了很多不同的方法来加入安全身份表来纠正这个问题,但是无法这样做..任何帮助都会非常感激!

以下是三个表格: pfm.task enter image description here

pfm.requestedtask enter image description here

core.securityidentity enter image description here

0 个答案:

没有答案