TSQL选择返回太多或太少行的问题。可能需要一个联盟或类似的

时间:2016-10-24 23:29:29

标签: sql-server tsql

下面是一个很好的选择,但我被要求添加另一列,它让我难过。

我需要将来自连接的dbo.DEPARTMENTS.NAME的部门名称添加到dbo.MOBILEUSER.USER_ID到dbo.MOBILEUSERDEPTS.USER_ID,给予DEPT_ID,DEPT_ID连接到DEPT_ID上的DEPARTMENTS,允许选择DEPARTMENT NAME为移动用户。另一个问题是虽然每一行都有一个Employee USER_ID,但并不是每一行都有一个MOBILE USER或DESKTOP用户。员工和移动用户也可能有多个DEPARTMENTS,因此可能有多行。 期望的输出是 员工员工员工EMPDEPT用户用户门户网站PORTALUSER Mobile USER_ID上的EMP_ID PORTALUSER上的员工 DEPT_ID上的DEPT_ID PORTALUSERDEPTS上的EMPDEPTS 部门部门 Department LastName FirstName EmployeeStatus DesktopUser DesktopStatus PortalUser PortalStatus Department 样品 Spokane Doe John Active NULL NULL JDOE Active Seattle 伦敦史密斯大卫活跃David.Smythe活跃的DSMYTHE InActive牛津 Oxford Smythe David Active NULL NULL NULL NULL NULL

源表结构 员工EMP_ID         姓         名字 EMPDEPT EMP_ID         DEPT_ID         状态状态T / A(终止/活动) DEPARTMENTS DEPT_ID         名称 USER USER_ID         用户名         ACTIVE状态T / F. PORTALUSER USER_ID         姓         名字         用户名         批准状态T / F. PORTALUSERDEPTS USER_ID         DEPT_ID

我遇到的问题是移动用户部门和员工部门 - 到目前为止我只能得到一个或者另一个。     enter image description here SELECT
        dbo.DEPARTMENTS.NAME为部门,dbo.STAFF.LASTNAME为员工,         StaffStatus = CASE dbo.STFDEPT.STATUS                          当'T'然后'终止'                          当'A'然后'活跃'                          ELSE'警告!!'                       结束,         dbo.USERS.USERNAME AS桌面,         DeskTopStatus = CASE dbo.USERS.ACTIVE                            当'T'然后'活跃'                            当'F'然后'无效'                            ELSE''                         结束,         dbo.MOBILEUSERS.USERNAME AS MOBUser,         MobileStatus = CASE dbo.PORTALUSERS.APPROVED                           当'T'然后'活跃'                           当'F'然后'无效'                           ELSE''                        结束     从
        dbo.STAFF     内部联接         dbo.STFDEPT ON dbo.STAFF.EMP_ID = dbo.STFDEPT.EMP_ID     正确加入         dbo.DEPARTMENTS on dbo.STFDEPT.DEPT_ID = dbo.DEPARTMENTS.DEPT_ID     LEFT OUTER JOIN         dbo.USERS ON dbo.DEPARTMENTS.DEPT_ID = dbo.USERS.DEFDEPT                   AND dbo.STAFF.EMP_ID = dbo.USERS.USER_ID     LEFT OUTER JOIN         dbo.PORTALUSERS在dbo.STAFF.EMAILADDR = dbo.PORTALUSERS.EMAIL     哪里         dbo.STAFF.LASTNAME不喜欢'STAFF%'         和dbo.STAFF.LASTNAME不喜欢'Admin%'     订购         2,1

SELECT        
    DEPARTMENTS_1.NAME AS Department, 
    dbo.EMPLOYEES.LASTNAME AS Employee, 
    CASE dbo.EMPLDEPT.STATUS 
       WHEN 'T' THEN 'Terminated' 
       WHEN 'A' THEN 'Active' 
       ELSE 'WARNING!!' 
    END AS EmployeeStatus, 
    dbo.USERS.USERNAME AS Desktop, 
    CASE dbo.USERS.ACTIVE 
       WHEN 'T' THEN 'Active' 
       WHEN 'F' THEN 'Inactive' 
       ELSE '' 
    END AS DeskTopStatus, 
    dbo.PORTALUSERS.USERNAME AS PortUser, 
    CASE dbo.PORTALUSERS.APPROVED 
       WHEN 'T' THEN 'Active' 
       WHEN 'F' THEN 'Inactive' 
       ELSE '' 
    END AS MobileStatus
FROM
    dbo.EMPLOYEES 
INNER JOIN
    dbo.EMPLDEPT ON dbo.EMPLOYEES.EMP_ID = dbo.EMPLDEPT.EMP_ID 
INNER JOIN
    dbo.DEPARTMENTS ON dbo.EMPLDEPT.DEPT_ID = dbo.DEPARTMENTS.DEPT_ID 
INNER JOIN
    dbo.PORTALUSERDEPTS ON dbo.DEPARTMENTS.DEPT_ID = dbo.PORTALUSERDEPTS.DEPT_ID 
LEFT OUTER JOIN
    dbo.PORTALUSERS ON dbo.PORTALUSERDEPTS.USER_ID = dbo.PORTALUSERS.USER_ID 
                    AND dbo.EMPLOYEES.EMAILADDR = dbo.PORTALUSERS.EMAIL 
RIGHT OUTER JOIN
    dbo.DEPARTMENTS AS DEPARTMENTS_1 ON dbo.PORTALUSERDEPTS.DEPT_ID = DEPARTMENTS_1.DEPT_ID 
                                     AND dbo.EMPLDEPT.DEPT_ID = DEPARTMENTS_1.DEPT_ID 
LEFT OUTER JOIN
    dbo.USERS ON dbo.DEPARTMENTS.DEPT_ID = dbo.USERS.DEFDEPT 
              AND dbo.EMPLOYEES.EMP_ID = dbo.USERS.USER_ID 
              AND DEPARTMENTS_1.DEPT_ID = dbo.USERS.DEFDEPT
WHERE        
    (dbo.EMPLOYEES.LASTNAME NOT LIKE '99%') 
    AND (dbo.EMPLOYEES.LASTNAME NOT LIKE 'Contractor%')
ORDER BY 
    Employee, Department

0 个答案:

没有答案