我正在尝试实现此查询,但我无法弄清楚为什么我没有得到结果。
以下是说明:
假设我有一个电话会议:TableAct
Acct# Date WithdrawAmt DepositAmt
!24455 2012-11-19-00.00.00 1245.77 200.50
125577 2011-02-12-00.00.00 100.98 578.00
另一张表TableCustomerOrd
:
ID# COrder# CustID Ord_Description VendorType
124455 7712AS 123 1AAA Permanent
125577 9914DL 346 1BBB Partial
... UK1234 111 2HJ5 Permanent'
,,, FR0912 567 5LGY Partial
然后TableCustomerDtls
:
CustID Descriptions Delivery Address ZipCode
123 1AAA_BLUESHARE SUCCESSFUL 222 Main St 97002
346 1BBB_CHASE DECLINE 40 West Side 97122
111 2HJ5_CITIBANK SUCCESSFUL ……. …….
567 5LGY_VANGURD DECLINED ---- -----
表DelivaryFlight
:
FlightOrder# FlightCustID FlightDt
7712AS 123 2011-9-29-00.00.00
9914DL 346 2010-11-2-00.00.00
UK1234 111 2012-4-1-00.00.00
FR0912 567 2012-9-11-00.00.00
我想在以下条件下更新TableAct
:
TableAct. Acct# = TableCustomerOrd.ID#
,AND
:TableCustomerOrd. CustID = TableCustomerDtls.CustID
同时,TableCustomerOrd.Ord_Descriptions
字段应与TableCustomerDtls. Descriptions
字段匹配“_”
之前的任何字段。因此‘1AAA’, ‘2HJ5’
等AND
: DelivaryFlight.FlightOrder# = TableCustomerOrd.COrder#
,AND
:DelivaryFlight.FlightCustID = TableCustomerOrd. CustID
。另外TableCustomerDtls. Delivery = ‘SUCCESSFUL’
AND:
DelivaryFlight.FlightOrder# = TableCustomerOrd. COrder#
AND DelivaryFlight.FlightCustID = TableCustomerOrd. CustID
Also TableCustomerDtls. Delivery = ‘DECLINED
然后我要比较:elivaryFlight.FlightDt > DelivaryFlight.FlightDt
。
基本上我需要将表DelivaryFlight
列FlightOrder#, FlightCustID
与TableCustomerOrd
匹配。
TableCustomerDtls
Delivery
列ck
delivary
‘DECLINED’
‘SUCCESSFUL’
状态,例如‘SUCCESSFUL’ FlightDt
。
‘DECLINED’ FlightDt
条件并将 Update
Set …
FROM TableAct AC
Join TableCustomerOrd CustOd
ON AC.Acct# = CustOd.ID#
Join TableCustomerDtls CDtls
ON CDtls. CustID = CustOd. CustID
AND (CustOd.Ord_Descriptions =
Left(CDtls.Descriptions, LEN(rtrim(CDtls.Descriptions))))
JOIN DelivaryFlight DF
ON DF.FlightOrder# = CustOd.COrder#
AND DF.FlightCustID = CustOd.CustID
AND CDtls.Delivery = ‘SUCCESSFUL’
JOIN DelivaryFlight DF2
ON DF2.FlightOrder# = DF.COrder#
AND DF2.FlightCustID = DF.CustID
AND CDtls.Delivery = ‘DECLINED’
WHERE DelivaryFlight. FlightDt > DelivaryFlight. FlightDt
AND DepositAmt > 100
与{{1}}进行比较。
这是我的查询,但请帮助我理解,我相信这可以以更好的方式完成。
查询无效:
{{1}}
你的帮助将是巨大的'因为我的项目将在本周结束。
谢谢你
答案 0 :(得分:0)
如果我有这样的复杂查询,我首先创建一个“简单”选择,它只生成要更新的行。
它还应该返回更新值和更新表的pk
然后(相对)直接将(内部)连接到要更新的表并进行更新记忆以仅通过包括
来更新匹配的行WHERE tblTobeUpdated.pk = SimpleSelect.pk
希望这有帮助
答案 1 :(得分:0)
我没有时间深入研究这个问题,但我怀疑你至少想要解决这个问题:
WHERE DelivaryFlight. FlightDt > DelivaryFlight. FlightDt
这是一个永远无法满足的条件。
你可能想要:
WHERE DF. FlightDt > DF2. FlightDt
对于这些复杂的queires,更新能够查看将要更新的记录也很有用,所以我通常会这样做:
Update
Set …
--Select *
FROM TableAct AC
然后我没有运行更新,而是只运行突出显示并运行以select开头的部分以查看结果,直到我确定选择要选择的记录并且值为值时才测试更新我将替换是正确的。
答案 2 :(得分: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