TSQL -

2017-12-28 25 views
0

を支払った総労働時間とお金は、私は次のコードを持っている:TSQL -

USE databse_Live 

GO 


DECLARE @Site INT; SET @Site = 38; 
DECLARE @DateFrom DATETIME, @DateTo DATETIME; 
SET @DateFrom = '2017-09-01'; 
SET @DateTo = '2017-12-21'; 

SELECT 
     w.[PayrollRef], 
     w.[FirstName], 
     w.[LastName], 
     r1s.[NationalInsurance], 
     SUM(CASE WHEN Day1_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day2_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day3_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day4_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day5_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day6_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day7_Standard_Hours >= 0 THEN 1 ELSE 0 END) TotalDays,  
     --SUM( Day1_Standard_Hours + Day2_Standard_Hours + Day3_Standard_Hours + Day4_Standard_Hours + Day5_Standard_Hours + Day6_Standard_Hours + Day7_Standard_Hours + 
      --Day1_Overtime_Hours + Day2_Overtime_Hours + Day3_Overtime_Hours + Day4_Overtime_Hours + Day5_Overtime_Hours + Day6_Overtime_Hours + Day7_Overtime_Hours + 
      --Day1_Adhoc_Hours + Day2_Adhoc_Hours + Day3_Adhoc_Hours + Day4_Adhoc_Hours + Day5_Adhoc_Hours + Day6_Adhoc_Hours + Day7_Adhoc_Hours) AS TotalHours, 
     CAST(SUM(ISNULL(r1s.[PayRate] * tl.[Day1_Standard_Hours], 0) + 
      ISNULL(r2s.[PayRate] * tl.[Day2_Standard_Hours], 0) + 
      ISNULL(r3s.[PayRate] * tl.[Day3_Standard_Hours], 0) + 
      ISNULL(r4s.[PayRate] * tl.[Day4_Standard_Hours], 0) + 
      ISNULL(r5s.[PayRate] * tl.[Day5_Standard_Hours], 0) + 
      ISNULL(r6s.[PayRate] * tl.[Day6_Standard_Hours], 0) + 
      ISNULL(r7s.[PayRate] * tl.[Day7_Standard_Hours], 0) + 
      ISNULL(r1O.[PayRate] * tl.[Day1_Overtime_Hours], 0) + 
      ISNULL(r2O.[PayRate] * tl.[Day2_Overtime_Hours], 0) + 
      ISNULL(r3O.[PayRate] * tl.[Day3_Overtime_Hours], 0) + 
      ISNULL(r4O.[PayRate] * tl.[Day4_Overtime_Hours], 0) + 
      ISNULL(r5O.[PayRate] * tl.[Day5_Overtime_Hours], 0) + 
      ISNULL(r6O.[PayRate] * tl.[Day6_Overtime_Hours], 0) + 
      ISNULL(r7O.[PayRate] * tl.[Day7_Overtime_Hours], 0) + 
      ISNULL(r1a.[PayRate] * tl.[Day1_Adhoc_Hours], 0) + 
      ISNULL(r2a.[PayRate] * tl.[Day2_Adhoc_Hours], 0) + 
      ISNULL(r3a.[PayRate] * tl.[Day3_Adhoc_Hours], 0) + 
      ISNULL(r4a.[PayRate] * tl.[Day4_Adhoc_Hours], 0) + 
      ISNULL(r5a.[PayRate] * tl.[Day5_Adhoc_Hours], 0) + 
      ISNULL(r6a.[PayRate] * tl.[Day6_Adhoc_Hours], 0) + 
      ISNULL(r7a.[PayRate] * tl.[Day7_Adhoc_Hours], 0)) AS Decimal (10, 2)) AS [TotalPay], 
     CAST(SUM(ISNULL((r1s.[PayRate] + r1s.[WorkingTimeRegulation] + r1s.[Margin] + r1s.[pension] + r1s.[NationalInsurance]) * [Day1_standard_Hours], 0) + 
      ISNULL((r2s.[PayRate] + r2s.[WorkingTimeRegulation] + r2s.[Margin] + r2s.[Pension] + r2s.[NationalInsurance]) * [Day2_Standard_Hours], 0) + 
      ISNULL((r3s.[PayRate] + r3s.[WorkingTimeRegulation] + r3s.[Margin] + r3s.[Pension] + r3s.[NationalInsurance]) * [Day3_Standard_Hours], 0) + 
      ISNULL((r4s.[PayRate] + r4s.[WorkingTimeRegulation] + r4s.[Pension] + r4s.[Margin] + r4s.[NationalInsurance]) * [Day4_Standard_Hours], 0) + 
      ISNULL((r5s.[PayRate] + r5s.[WorkingTimeRegulation] + r5s.[Pension] + r5s.[Margin] + r5s.[NationalInsurance]) * [Day5_Standard_Hours], 0) + 
      ISNULL((r6s.[PayRate] + r6s.[WorkingTimeRegulation] + r6s.[Pension] + r6s.[Margin] + r6s.[NationalInsurance]) * [Day6_Standard_Hours], 0) + 
      ISNULL((r7s.[PayRate] + r7s.[WorkingTimeRegulation] + r7s.[Pension] + r7s.[Margin] + r7s.[NationalInsurance]) * [Day7_Standard_Hours], 0) + 
      ISNULL((r1o.[PayRate] + r1o.[WorkingTimeRegulation] + r1o.[Margin] + r1o.[pension] + r1o.[NationalInsurance]) * [Day1_Overtime_Hours], 0) + 
      ISNULL((r2o.[PayRate] + r2o.[WorkingTimeRegulation] + r2o.[Margin] + r2o.[Pension] + r2o.[NationalInsurance]) * [Day2_Overtime_Hours], 0) + 
      ISNULL((r3o.[PayRate] + r3o.[WorkingTimeRegulation] + r3o.[Margin] + r3o.[Pension] + r3o.[NationalInsurance]) * [Day3_Overtime_Hours], 0) + 
      ISNULL((r4o.[PayRate] + r4o.[WorkingTimeRegulation] + r4o.[Pension] + r4o.[Margin] + r4o.[NationalInsurance]) * [Day4_Overtime_Hours], 0) + 
      ISNULL((r5o.[PayRate] + r5o.[WorkingTimeRegulation] + r5o.[Pension] + r5o.[Margin] + r5o.[NationalInsurance]) * [Day5_Overtime_Hours], 0) + 
      ISNULL((r6o.[PayRate] + r6o.[WorkingTimeRegulation] + r6o.[Pension] + r6o.[Margin] + r6o.[NationalInsurance]) * [Day6_Overtime_Hours], 0) + 
      ISNULL((r7o.[PayRate] + r7o.[WorkingTimeRegulation] + r7o.[Pension] + r7o.[Margin] + r7o.[NationalInsurance]) * [Day7_Overtime_Hours], 0) + 
      ISNULL((r1a.[PayRate] + r1a.[WorkingTimeRegulation] + r1a.[Margin] + r1a.[pension] + r1a.[NationalInsurance]) * [Day1_Adhoc_Hours], 0) + 
      ISNULL((r2a.[PayRate] + r2a.[WorkingTimeRegulation] + r2a.[Margin] + r2a.[Pension] + r2a.[NationalInsurance]) * [Day2_Adhoc_Hours], 0) + 
      ISNULL((r3a.[PayRate] + r3a.[WorkingTimeRegulation] + r3a.[Margin] + r3a.[Pension] + r3a.[NationalInsurance]) * [Day3_Adhoc_Hours], 0) + 
      ISNULL((r4a.[PayRate] + r4a.[WorkingTimeRegulation] + r4a.[Pension] + r4a.[Margin] + r4a.[NationalInsurance]) * [Day4_Adhoc_Hours], 0) + 
      ISNULL((r5a.[PayRate] + r5a.[WorkingTimeRegulation] + r5a.[Pension] + r5a.[Margin] + r5a.[NationalInsurance]) * [Day5_Adhoc_Hours], 0) + 
      ISNULL((r6a.[PayRate] + r6a.[WorkingTimeRegulation] + r6a.[Pension] + r6a.[Margin] + r6a.[NationalInsurance]) * [Day6_Adhoc_Hours], 0) + 
      ISNULL((r7a.[PayRate] + r7a.[WorkingTimeRegulation] + r7a.[Pension] + r7a.[Margin] + r7a.[NationalInsurance]) * [Day7_Adhoc_Hours], 0)) AS decimal(10, 2)) AS [TotalCharge] 


    FROM [TimesheetLine] tl 

    LEFT OUTER JOIN [Timesheet]  ts  ON  tl.[TimesheetId] = ts.[Id] 
    LEFT OUTER JOIN [Worker]  w  ON  tl.[WorkerId] = w.[Id] 
    LEFT OUTER JOIN [Rate]   r1s  ON  tl.[Day1_Standard_RateId] = r1s.[id] 
    LEFT OUTER JOIN [Rate]   r2s  ON  tl.[Day2_Standard_RateId] = r2s.[Id] 
    LEFT OUTER JOIN [Rate]   r3s  ON  tl.[Day3_Standard_RateId] = r3s.[Id] 
    LEFT OUTER JOIN [Rate]   r4s  ON  tl.[Day3_Standard_RateId] = r4s.[Id] 
    LEFT OUTER JOIN [Rate]   r5s  ON  tl.[Day3_Standard_RateId] = r5s.[Id] 
    LEFT OUTER JOIN [Rate]   r6s  ON  tl.[Day3_Standard_RateId] = r6s.[Id] 
    LEFT OUTER JOIN [Rate]   r7s  ON  tl.[Day3_Standard_RateId] = r7s.[Id] 
    LEFT OUTER JOIN [Rate]   r1o  ON  tl.[Day1_Overtime_RateId] = r1o.[id] 
    LEFT OUTER JOIN [Rate]   r2o  ON  tl.[Day2_Overtime_RateId] = r2o.[id] 
    LEFT OUTER JOIN [Rate]   r3o  ON  tl.[Day3_Overtime_RateId] = r3o.[id] 
    LEFT OUTER JOIN [Rate]   r4o  ON  tl.[Day4_Overtime_RateId] = r4o.[id] 
    LEFT OUTER JOIN [Rate]   r5o  ON  tl.[Day5_Overtime_RateId] = r5o.[id] 
    LEFT OUTER JOIN [Rate]   r6o  ON  tl.[Day6_Overtime_RateId] = r6o.[id] 
    LEFT OUTER JOIN [Rate]   r7o  ON  tl.[Day7_Overtime_RateId] = r7o.[id] 
    LEFT OUTER JOIN [Rate]   r1a  ON  tl.[Day1_Adhoc_RateId] = r1a.[id] 
    LEFT OUTER JOIN [Rate]   r2a  ON  tl.[Day2_Adhoc_RateId] = r2a.[id] 
    LEFT OUTER JOIN [Rate]   r3a  ON  tl.[Day3_Adhoc_RateId] = r3a.[id] 
    LEFT OUTER JOIN [Rate]   r4a  ON  tl.[Day4_Adhoc_RateId] = r4a.[id] 
    LEFT OUTER JOIN [Rate]   r5a  ON  tl.[Day5_Adhoc_RateId] = r5a.[id] 
    LEFT OUTER JOIN [Rate]   r6a  ON  tl.[Day6_Adhoc_RateId] = r6a.[id] 
    LEFT OUTER JOIN [Rate]   r7a  ON  tl.[Day7_Adhoc_RateId] = r7a.[id] 

WHERE 
    ts.[SiteId] = @Site 
    AND ts.[datefrom] BETWEEN @DateFrom AND @DateTo 


GROUP BY w.[FirstName], w.[LastName], w.[PayrollRef], r1s.[NationalInsurance] 

ORDER BY TotalDays ASC 

しかし、一部の人は5日働いていることを私に示しているが、彼らは時間や有給お金がありません。例えば。

Results of the query

このクエリを実行するの全体のポイントは、彼らが全電荷に支払われた日数労働者が働いており、何かを見つけることです。私は正しい日々が働いている正しい労働者を得ています。しかし、合計時間または合計金額が計算されているときは0で表示すべきではないので間違っており、合計は同じです。

+0

いくつかのサンプル入力と所望の出力を与える... – Ajay

+0

サンプル入力は、標準、残業又はアドホック時間かどうか、作業者の時間を記録するデータベース内のタイムシートから採取したデータです。各時間タイプは、個々のレートIDに関連付けられています。上記の3つの値が偽で残りが真であることを示すスクリーンショットの上に希望の出力が表示されます –

答えて

0

計算のいくつかの値は、 totalpaidとtotal chargeの結果はゼロになります。

以下のスクリプトを個別に選択してバグを知るようにしてください。

SELECT r1s.[PayRate] * tl.[Day1_Standard_Hours], 
r2s.[PayRate] * tl.[Day2_Standard_Hours], 
       r3s.[PayRate] * tl.[Day3_Standard_Hours], 
       r4s.[PayRate] * tl.[Day4_Standard_Hours], 
       r5s.[PayRate] * tl.[Day5_Standard_Hours], 
       r6s.[PayRate] * tl.[Day6_Standard_Hours], 
       r7s.[PayRate] * tl.[Day7_Standard_Hours], 
r1O.[PayRate] * tl.[Day1_Overtime_Hours], 
       r2O.[PayRate] * tl.[Day2_Overtime_Hours], 
       r3O.[PayRate] * tl.[Day3_Overtime_Hours], 
       r4O.[PayRate] * tl.[Day4_Overtime_Hours], 
       r5O.[PayRate] * tl.[Day5_Overtime_Hours], 
       r6O.[PayRate] * tl.[Day6_Overtime_Hours], 
       r7O.[PayRate] * tl.[Day7_Overtime_Hours], 
       r1a.[PayRate] * tl.[Day1_Adhoc_Hours], 
       r2a.[PayRate] * tl.[Day2_Adhoc_Hours], 
       r3a.[PayRate] * tl.[Day3_Adhoc_Hours], 
       r4a.[PayRate] * tl.[Day4_Adhoc_Hours], 
       r5a.[PayRate] * tl.[Day5_Adhoc_Hours], 
       r6a.[PayRate] * tl.[Day6_Adhoc_Hours], 
       r7a.[PayRate] * tl.[Day7_Adhoc_Hours] 
0

今設定している方法は、ゼロ時間を1日として処理した日を含みます。ユーザーは5日を表示しているかもしれませんが、その日は0時間です。そのため、有料レートを掛け合わせると、獲得したゼロドルが得られます。

SELECT 
     w.[PayrollRef], 
     w.[FirstName], 
     w.[LastName], 
     r1s.[NationalInsurance], 
     SUM(CASE WHEN Day1_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day2_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day3_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day4_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day5_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day6_Standard_Hours >= 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day7_Standard_Hours >= 0 THEN 1 ELSE 0 END) TotalDays, 

クエリをゼロより大きく大きくないように変更する必要があります。

SELECT 
     w.[PayrollRef], 
     w.[FirstName], 
     w.[LastName], 
     r1s.[NationalInsurance], 
     SUM(CASE WHEN Day1_Standard_Hours > 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day2_Standard_Hours > 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day3_Standard_Hours > 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day4_Standard_Hours > 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day5_Standard_Hours > 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day6_Standard_Hours > 0 THEN 1 ELSE 0 END) + 
     SUM(CASE WHEN Day7_Standard_Hours > 0 THEN 1 ELSE 0 END) TotalDays,