问问题
178 次
3 回答
0
我没有时间深入研究这个问题,但我怀疑你至少想要修复:
WHERE DelivaryFlight. FlightDt > DelivaryFlight. FlightDt
这是永远无法满足的条件。
你可能想要:
WHERE DF. FlightDt > DF2. FlightDt
对于这些复杂的查询来说,更新能够看到将要更新的记录也很有用,所以我通常会这样做:
Update
Set …
--Select *
FROM TableAct AC
然后,我没有运行更新,而是只运行突出显示并运行以 select 开头的部分以查看结果,并且在我确定我正在选择我想要选择的记录并且我将成为的值之前不要测试更新更换是正确的。
于 2012-12-11T20:05:38.810 回答
0
试着分解你的查询,这是我今天写的一个查询,分别测试每个部分
SELECT
Employee
, Reference
, Payroll
, [Hours] / 60
[Hours]
, [Days]
FROM
(
SELECT
Employee
, Reference
, Payroll
, SUM( Duration ) AS [Hours]
, AvailableID
FROM
(
SELECT
RequirerID
, Duration
, RTRIM( COALESCE(MA.MemberLastName, '')
+ ' ' + COALESCE(MA.MemberFirstName, '')
+ ' ' + COALESCE(MA.MemberInitial, '')) Employee
, COALESCE(MA.Detailref1, '') Reference
, COALESCE(MA.PayrollRef, '') Payroll
, Available.AvailableId
FROM
(
SELECT DISTINCT
RequirerID
, ShiftDate
, CAST(ShiftStart - ShiftEnd - ShiftBreak AS DECIMAL(19,2)) ShiftDuration
, Id RequirementRecordID
FROM
Requirements
WHERE
Requirements.ShiftDate BETWEEN @ParamStartDate
AND @ParamEndDate
AND RequirerID IN (SELECT ID FROM MemberDetails WHERE CompanyID = @ParamCompanyID)
)
R
INNER JOIN
ShiftConfirmed
INNER JOIN
Available
INNER JOIN
MemberDetails MA
ON Available.AvailableID = MA.ID
ON ShiftConfirmed.AvailableRecordID = Available.ID
ON R.RequirementRecordID = ShiftConfirmed.RequirementRecordID
WHERE
R.ShiftDate BETWEEN @ParamStartDate
AND @ParamEndDate
AND COALESCE(ShiftChecked, 0) BETWEEN 0 AND 1
)
ShiftDay
Group By
Employee
, Reference
, Payroll
, AvailableId
) Shifts
INNER JOIN
(
SELECT
COUNT( * ) AS [Days]
, AvailableID
FROM
(
SELECT DISTINCT
R.ShiftDate
, Available.AvailableId
FROM
(
SELECT DISTINCT
ShiftDate
, Id RequirementRecordID
FROM
Requirements
WHERE
Requirements.ShiftDate BETWEEN @ParamStartDate
AND @ParamEndDate
AND RequirerID IN (SELECT ID FROM MemberDetails WHERE CompanyID = @ParamCompanyID)
)
R
INNER JOIN
ShiftConfirmed
INNER JOIN
Available
INNER JOIN
MemberDetails MA
ON Available.AvailableID = MA.ID
ON ShiftConfirmed.AvailableRecordID = Available.ID
ON R.RequirementRecordID = ShiftConfirmed.RequirementRecordID
WHERE
R.ShiftDate BETWEEN @ParamStartDate
AND @ParamEndDate
AND COALESCE(ShiftChecked, 0) BETWEEN 0 AND 1
)
ShiftDay
Group By
AvailableId
) D
ON Shifts.AvailableID = D.AvailableID
WHERE [Hours] > 0
ORDER BY
Employee
于 2012-12-13T09:14:14.653 回答
0
如果我有这样一个复杂的查询,我首先创建一个“简单”选择,它只生成要更新的行。
它还应该返回更新值和更新表的 pk
然后(相对)直接(内部)将它与要更新的表连接起来,并通过包括
WHERE tblTobeUpdated.pk = SimpleSelect.pk
希望这可以帮助
于 2012-12-11T17:11:34.560 回答