高级SQL查询,需要添加另一个表和员工ID

时间:2015-04-22 14:12:54

标签: sql join inner-join outer-join outsystems

我是高级SQL的新手。我有一个查询,我需要添加一个变量。它需要连接到employee表,并且只显示该特定employeeId的记录。这是Outsystems和高级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

2 个答案:

答案 0 :(得分:2)

您需要向查询添加新参数(EmployeeId,可能是EmployeeId Identifier类型)并将条件添加到查询中。

enter image description here

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

  • 只在最外面的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(...或...)包装备用条件

答案 1 :(得分:1)

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

Outsystems的论坛是一种知识的字体! (给他们一个功劳!)

相关问题