2016-04-09 2 views
0

週に136 合計OT時間:週変数と条件文を使用して時間を計算するSQLクエリは、間違った出力を与えています。このクエリは5つのクエリのうち、どれも間違った出力を返します。

合計定期時間:週544 総休日の時間、私はこの出力を取得しようとしています26

しかし、代わりにこれを取得しています:

警告:Null値は、集計または他のSET操作によって削除されます。 今週は休日の支払いがありませんでした。

はここに私のSQLコードです:

--Declare 5 int variables 
    DECLARE @TotHours int, @TotEmps int, @HolHours int, @RegHours int, @OTHours int 

--Set the Total Hours variable = to the sum total of workHours where the payperiod ID is most recent 
SET @TotHours = (SELECT SUM(WorkHours) FROM Hours WHERE PPID = (SELECT MAX(PPID) FROM PayPeriod)) 

--Set the Holiday Hours variable = to the sum total of HolHours where the payperiod ID is most recent 
SET @HolHours = (SELECT SUM(@HolHours) FROM Hours WHERE PPID = (SELECT MAX(PPID) FROM PayPeriod)) 

--Set the Total Employees variable = to the count of employee ID's where there is no TermDate 
SET @TotEmps = (SELECT COUNT(EmpData.EmpID) FROM EmpData JOIN Work ON EmpData.EmpID = Work.EmpID WHERE EndDate IS NULL) 

--Set the Regular Hours variable = to the Total Employees variable * 32 
SET @RegHours = (SELECT SUM(@TotEmps) * 32); 


--(USE THIS CONDITIONAL STRUCTURE- substitute variable names where needed and remove the "--") 
IF @HolHours > 0 
    BEGIN 
     IF @RegHours > @TotHours 
      BEGIN 
      --set the Overtime Hours variable = 0 
    SET @OTHours = 0 
      --set the Regular Hours variable = Total Hours variable 
    SET @RegHours = @TotHours 

      --Place the 3 print statements here 
    PRINT 'Total regular hours for the week: @TotHours.' 
    PRINT 'Total holiday hours for the week: @HolHours.' 
    PRINT 'Total OT Hours for the week: @OTHours.' 
      END 
     ELSE 
      BEGIN 
      --set the Overtime Hours variable = Total Hours variable - Regular Hours variable 
    SET @OTHours = @TotHours - @RegHours 

      --place the 3 print statements here 

    PRINT 'Total regular hours for the week: @TotHours.' 
    PRINT 'Total holiday hours for the week: @HolHours.' 
    PRINT 'Total OT Hours for the week: @OTHours.' 
      END 
      END 
ELSE 
    --Place the single print statement here (doesn't use variables) 
    PRINT 'This week had no holiday pay.' 

任意の助けをいただければ幸いです、ありがとうございました。

答えて

1

休日の時間の合計は間違っている:

SET @HolHours = (SELECT SUM(@HolHours) FROM Hours WHERE PPID = (SELECT MAX(PPID) FROM PayPeriod)) 

多分これは、これは次のようになります。

SET @HolHours = (SELECT SUM(HolHours) FROM Hours WHERE PPID = (SELECT MAX(PPID) FROM PayPeriod)) 
+0

感謝。私はまた、印刷文でvarcharに変換するのを忘れてしまった。今度は、RegHoursが544の代わりに570であることを除いて、正しい出力を得ています。うーん... – Pau808

+0

@ Pau808 SET @TotEmps =(SELECT COUNT(EmpData.EmpID)...)は正しいですか?私はあなたが高すぎるということを心配しています。おそらく 'SET @ TotEmps =(SELECT COUNT(DISTINCT EmpData.EmpID)... 'であるべきです。 – user212514

関連する問題