我有以下代码:
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天,但他们没有工作时间或没有钱。例如。
执行此查询的重点是找出工人工作了多少天以及他们用总费用支付了多少钱。我找到了合适的工作人员。但是,当计算总小时数或总金额时,它是错误的,因为它不应该显示为0并且总费用相同
答案 0 :(得分:0)
似乎通过将totalpaid和totalcharge的整个结果减少到零来计算计算中的某些值为null。
我建议你单独选择以下脚本,这样你就可以了解bug。
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]
答案 1 :(得分:0)
现在设置它的方式,包括零工作日为1天。用户可能会显示5天,但这些天的工作时间为零,所以当您将工资率乘以时,您将获得零美元。
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,