根据SQL Server中的条件添加一行

时间:2015-08-20 05:52:27

标签: sql-server

在SQL Server中,我有一个临时表,我想在每个员工下面添加一行。此处@STARTDATE是2015年8月1日,可以改变。

它应检查如下条件:

WHEN (JoiningDate of employee) > @STARTDATE THEN
   Case 
    When (EXPECTEDSTARTDATE(Its a project start date) = (JoiningDate of employee) Then NULL
    When (EXPECTEDSTARTDATE > (JoiningDate of employee) Then (Daysallocated)*8)-SUM(InstallCapacity))

  End
WHEN (JoiningDate of employee) = @STARTDATE THEN
  Case 
    When (EXPECTEDSTARTDATE = @STARTDATE) Then NULL
    When (EXPECTEDSTARTDATE > @STARTDATE) Then (Daysallocated)*8)-SUM(InstallCapacity))

  End

我的桌子是

enter image description here

我的结果为

enter image description here

我在下面添加了行

INSERT INTO #tempelevan 
SELECT EMPLOYEECODE, EMPLOYEENAME, 'Jayaraj Mariadhas Nadar' AS ReportingTo , 'Bench' AS PROJECTNAME, [Resource Pool] AS [Resource Pool], NULL AS ResourceStatus, ORGROLE AS ORGROLE,
[Resource DU] AS [Resource DU], [Resource DT] AS [Resource DT], NULL AS EXPECTEDSTARTDATE, NULL AS EXPECTEDENDDATE, NULL AS ResourcePercentage, NULL AS DaysAllocated, 
CASE 
 WHEN ((SELECT emp.joiningdate FROM tbl_pm_employee emp WHERE emp.employeecode=ft.EMPLOYEECODE) > @STARTDATE)
 THEN 
    CASE 
        WHEN (ft.EXPECTEDSTARTDATE = (SELECT emp.JoiningDate FROM tbl_pm_employee emp WHERE  emp.employeecode=ft.EMPLOYEECODE)) 
        THEN NULL 
        WHEN (ft.EXPECTEDSTARTDATE > (SELECT emp.JoiningDate FROM tbl_pm_employee emp WHERE  emp.employeecode=ft.EMPLOYEECODE)) 
        THEN ((SELECT dbo.countemployeebusinessdays(ft. employeecode, (SELECT emp.joiningdate FROM tbl_pm_employee emp WHERE emp.employeecode=ft.employeecode), @ENDDATE)*8)-sum(installcapacity)) 
    END
 WHEN ((SELECT emp.joiningdate FROM tbl_pm_employee emp WHERE emp.employeecode=ft.employeecode) = @STARTDATE)
 THEN 
    CASE 
        WHEN (ft.expectedstartdate = @STARTDATE) 
        THEN NULL 
        WHEN (ft.expectedstartdate > @STARTDATE) 
        THEN ((SELECT dbo.countemployeebusinessdays(ft. employeecode, @STARTDATE, @ENDDATE))*8)-sum(installcapacity)
    END
END installcapacity, 
(1 - SUM([ActualAllocationPercentage])) as ActualPercentage
FROM #tempelevan ft GROUP BY ft.EMPLOYEECODE, ft.EMPLOYEENAME, ft.[Resource Pool], ft.ORGROLE, ft.[Resource DU], ft.[Resource DT], ft.EXPECTEDSTARTDATE

在结果表中,对于特定员工,应该只有一行Bench,但我遇到多行问题。如果有多个项目分配给同一员工,则显示行。

此外,我想隐藏Install capacity为Null的行。

1 个答案:

答案 0 :(得分:0)

这不是答案。在这里添加,不能留在评论中。

INSERT INTO #tempelevan SELECT EMPLOYEECODE, EMPLOYEENAME, 17ayaraj Mariadhas Hadar' AS reportinglo , 'Bench' AS projectname, [Resource Pool] AS [Resource Pool], NULL AS resourcestatus, orgrole AS orgrole, [Resource DU] AS [Resource DU], [Resource DT] AS [Resource DT], NULL AS expectedstartdate, NULL AS expectedenddate, NULL AS resourcepercentage, NULL AS daysallocated,CASE
WHEN 
  ( 
         SELECT emp.joiningdate 
         FROM   tbl_pm_employee emp 
         WHERE  emp.employeecode=ft.employeecode) > @STARTDATE THEN 
  CASE 
  WHEN ( 
      ft.expectedstartdate = 
      ( 
             SELECT emp.joiningdate 
             FROM   tbl_pm_employee emp 
             WHERE  emp.employeecode=ft.employeecode) 
    ) 
    THEN 
    NULL 
  WHEN ( 
      ft.expectedstartdate > 
      ( 
             SELECT emp.joiningdate 
             FROM   tbl_pm_employee emp 
             WHERE  emp.employeecode=ft.employeecode) 
    ) 
    THEN 
    ( 
    ( 
           SELECT dbo.countemployeebusinessdays(ft. employeecode, 
                  ( 
                         SELECT emp. joiningdate 
                         FROM   tbl_pm_employee emp 
                         WHERE  emp.employeecode=ft.employeecode), @ENDDATE)*8)-sum(installcapacity)) 
  END --WHEN (select emp.JoiningDate from TBL_PM_EMPLOYEE emp where emp.EmployeeCode=ft.EMPLOYEECODE) < @STARTDATE   WHEN (select emp.JoiningDate from TBL_PM_EMPLOYEE emp where emp.EmployeeCode=ft.EMPLOYEECODE) = @STARTDATE THEN Case When (ft.EXPECTEDSTARTDATE = @STARTDATE) Then NULL When (ft.EXPECTEDSTARTDATE > @STARTDATE) Then ((select dbo.CountEmployeeBusinessDays(ft.EMPLOYEECODE, @STARTDATE, @ENDDATE)*6)-SUM(InstallCapacity)) 
END 
END 
installcapacity, (1 - sumuactualallocationpercentagell) AS actualpercentage FROM #tempelevan ft GROUP BY ft.employeecode, ft.employeename, ft.[Resource Pool], ft.orgrole, ft.[Resource DU], ft.[Resource DT], ft.expectedstartdate