1

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

2 回答 2

2

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

在此处输入图像描述

在不知道您的完整数据库结构的情况下:

  • 只在最外层 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 ... )

于 2015-04-23T08:39:07.783 回答
1

我认为您应该查看此处建议的机制(传入输入变量并设置“扩展”):http ://www.outsystems.com/forums/discussion/6103/advanced-query-with-search-parameters/

Outsystems 论坛是知识的源泉!(感谢他们!)

于 2015-04-22T14:24:56.393 回答