进阶SQL查询,需另加table及员工编号

Advanced SQL query, need to add another table and employee ID

我是高级新手 SQL。我有一个查询需要添加一个变量。它需要连接到员工 table 并且只显示该特定员工 ID 的记录。这是外系统和高级 SQL 查询。有什么想法吗?

这是我需要添加的:Employee.EmployeeId = EmployeeId 或 EmployeeId = NullIdentifier()

现有查询,我需要将上面的内容添加到一些方法中:

Select 
      EMPLOYEEDISPLAYNAME ,
      ISNULL(Sick.Total,0.00) as SickValue,
      ISNULL( Vacation.Total,0.00) as VacationValue
   from 
      {Employee} 
         left join 
         ( Select 
                 EMPLOYEEID,
                 Sum( DEPOSITVALUE ) - Sum( WITHDRAWLVALUE ) as Total
              from 
                 {TimeOffRegisterEntry}
                    join {TimeOffRegister} 
                       on {TimeOffRegisterEntry}.TimeOffRegisterId = {TimeOffRegister}.TimeOffRegisterId 
                    join {TimeOffYear} 
                       on {TimeOffRegister}.TimeOffYearId = {TimeOffYear}.TimeOffYearId 
              where 
                     TIMEOFFTYPE = @VacationType 
                 and {TimeOffYear}.[TimeOffYearId] = @Year 
              group by 
                 EMPLOYEEID 
              having 
                 Sum( DEPOSITVALUE ) - Sum( WITHDRAWLVALUE ) < 0
         ) as Vacation 
           on {Employee}.EmployeeId = Vacation.EMPLOYEEID 

         left join 
         ( Select 
                 EMPLOYEEID,
                 Sum( DEPOSITVALUE ) - Sum( WITHDRAWLVALUE ) as Total
              from 
                 {TimeOffRegisterEntry}   
                    join {TimeOffRegister}  
                       on {TimeOffRegister}.TimeOffRegisterId = {TimeOffRegisterEntry}.TimeOffRegisterId
                    join {TimeOffYear} 
                       on {TimeOffRegister}.TimeOffYearId = {TimeOffYear}.TimeOffYearId 
              where 
                     TIMEOFFTYPE = @SickType 
                 and {TimeOffYear}.[TimeOffYearId] = @Year 
              group by 
                 EMPLOYEEID 
              having 
                 Sum( DEPOSITVALUE ) - Sum( WITHDRAWLVALUE ) < 0
         ) as Sick
            on {Employee}.EmployeeId = Sick.EMPLOYEEID 

   where 
         Vacation.total is not null 
      or Sick.Total is not null

我认为你应该看看这里建议的机制(传入一个输入变量并设置 'expand'):http://www.outsystems.com/forums/discussion/6103/advanced-query-with-search-parameters/

Outsystems 论坛是知识的源泉! (他们的功劳!)

您需要向查询添加一个新参数(EmployeeId,可能是 EmployeeId 标识符类型)并将条件添加到您的查询。

不知道完整的数据库结构:

  • 只在最外层的Where条件添加就可以了
  • 可能还需要将其添加到内部查询/优化查询

最终查询看起来像

Select 
  EMPLOYEEDISPLAYNAME ,
  ISNULL(Sick.Total,0.00) as SickValue,
  ISNULL( Vacation.Total,0.00) as VacationValue
from 
  {Employee} 
     left join 
     ( Select 
             EMPLOYEEID,
             Sum( DEPOSITVALUE ) - Sum( WITHDRAWLVALUE ) as Total
          from 
             {TimeOffRegisterEntry}
                join {TimeOffRegister} 
                   on {TimeOffRegisterEntry}.TimeOffRegisterId = {TimeOffRegister}.TimeOffRegisterId 
                join {TimeOffYear} 
                   on {TimeOffRegister}.TimeOffYearId = {TimeOffYear}.TimeOffYearId 
          where 
                 TIMEOFFTYPE = @VacationType 
             and {TimeOffYear}.[TimeOffYearId] = @Year 
             AND (@EmployeeId = 0 OR EMPLOYEEID = @EmployeeId)
          group by 
             EMPLOYEEID 
          having 
             Sum( DEPOSITVALUE ) - Sum( WITHDRAWLVALUE ) < 0
     ) as Vacation 
       on {Employee}.EmployeeId = Vacation.EMPLOYEEID 

     left join 
     ( Select 
             EMPLOYEEID,
             Sum( DEPOSITVALUE ) - Sum( WITHDRAWLVALUE ) as Total
          from 
             {TimeOffRegisterEntry}   
                join {TimeOffRegister}  
                   on {TimeOffRegister}.TimeOffRegisterId = {TimeOffRegisterEntry}.TimeOffRegisterId
                join {TimeOffYear} 
                   on {TimeOffRegister}.TimeOffYearId = {TimeOffYear}.TimeOffYearId 
          where 
                 TIMEOFFTYPE = @SickType 
             and {TimeOffYear}.[TimeOffYearId] = @Year 
              AND (@EmployeeId = 0 OR EMPLOYEEID = @EmployeeId)
          group by 
             EMPLOYEEID 
          having 
             Sum( DEPOSITVALUE ) - Sum( WITHDRAWLVALUE ) < 0
     ) as Sick
        on {Employee}.EmployeeId = Sick.EMPLOYEEID 

where 
    (@EmployeeId = 0 OR {Employee}.[EmployeeId] = @EmployeeId)
    AND (
         Vacation.total is not null 
      or Sick.Total is not null
    )

在上面的代码片段中查找对@EmployeeId 的引用。

注意 AND ( ... or ... ) 包裹了替代条件