SQL 使用 NULL 条件加入多个字段
SQL join multiple fields w/ a NULL criteria
我想识别来自 table 的货物,这些货物的路线已经为特定路线建立了合同,但使用了错误的承运人。
我能够单独查询和识别 Route/Carrier 匹配的合同类型以及尚未为任何承运人签订路线合同的合同类型。分别地,我是这样做的:
SELECT s.*,c.*
FROM Shipments s
INNER JOIN Contracts c ON s.Route=c.Route and s.Carrier=c.Carrier;
SELECT s.*,c.*
FROM Shipments s
LEFT JOIN Contracts c ON s.Route=c.Route
WHERE c.Route IS NULL;
黄色突出显示的行,但是,我需要帮助解决。这是已经签约了 Route 的情况,但使用了错误的承运人,从而否定了谈判合同的努力(是的,这是一个真实世界的场景)。
我试过以下方法,但它们没有隔离黄线,这是我的目标。
SELECT s.*,c.*
FROM Shipments s
LEFT JOIN Contracts c ON s.Route=c.Route and s.Carrier!=c.Carrier;
SELECT s.*,c.*
FROM Shipments s
LEFT JOIN Contracts c ON s.Route=c.Route and s.Carrier=c.Carrier
WHERE c.Carrier IS NULL;
SELECT s.*,c.*
FROM Shipments s
LEFT JOIN Contracts c ON s.Route=c.Route
WHERE c.Carrier IS NULL;
我正在尝试使用 ADODB 在 Excel VBA 中实现这一点,但目前,我真的很想从概念上理解如何在 [=32] 中实现这一点=]——如果可能的话——或者这样做的更好策略是什么。提前感谢您的帮助。
我想这就是你想要做的:
select distinct s.*
, c1.*
, case when c1.carrier = c2.carrier
then 'Correct carrier'
when s.carrier <> c2.carrier
then 'wrong carrier'
else 'No contract'
end as contractType
from Shipments s
left join Contracts c1
on s.route = c1.route
and s.carrier = c1.carrier
left join Contracts c2
on s.route = c2.route
where (
c1.route = c2.route
and c1.carrier = c2.carrier
)
or ( c1.route is null )
SELECT s.*,c.*
FROM Shipments s
LEFT JOIN Contracts c ON s.Route=c.Route and s.Carrier=c.Carrier;
如果 c.Route
为空,则这些货件没有合同。
你需要一个额外的连接来查看路由是否有契约然后CASE
逻辑:
SELECT s.*,
(CASE WHEN cr.route IS NULL THEN 'Route has no contract'
WHEN c.route IS NULL THEN 'Route assigned to wrong carrier'
ELSE 'Correct'
END)
FROM Shipments s LEFT JOIN
Contracts c
ON s.Route = c.Route AND s.carrier = c.carrier LEFT JOIN
(SELECT DISTINCT c.Route
FROM Contracts c
) cr
ON cr.Route = s.Route
我想识别来自 table 的货物,这些货物的路线已经为特定路线建立了合同,但使用了错误的承运人。
我能够单独查询和识别 Route/Carrier 匹配的合同类型以及尚未为任何承运人签订路线合同的合同类型。分别地,我是这样做的:
SELECT s.*,c.*
FROM Shipments s
INNER JOIN Contracts c ON s.Route=c.Route and s.Carrier=c.Carrier;
SELECT s.*,c.*
FROM Shipments s
LEFT JOIN Contracts c ON s.Route=c.Route
WHERE c.Route IS NULL;
黄色突出显示的行,但是,我需要帮助解决。这是已经签约了 Route 的情况,但使用了错误的承运人,从而否定了谈判合同的努力(是的,这是一个真实世界的场景)。
我试过以下方法,但它们没有隔离黄线,这是我的目标。
SELECT s.*,c.*
FROM Shipments s
LEFT JOIN Contracts c ON s.Route=c.Route and s.Carrier!=c.Carrier;
SELECT s.*,c.*
FROM Shipments s
LEFT JOIN Contracts c ON s.Route=c.Route and s.Carrier=c.Carrier
WHERE c.Carrier IS NULL;
SELECT s.*,c.*
FROM Shipments s
LEFT JOIN Contracts c ON s.Route=c.Route
WHERE c.Carrier IS NULL;
我正在尝试使用 ADODB 在 Excel VBA 中实现这一点,但目前,我真的很想从概念上理解如何在 [=32] 中实现这一点=]——如果可能的话——或者这样做的更好策略是什么。提前感谢您的帮助。
我想这就是你想要做的:
select distinct s.*
, c1.*
, case when c1.carrier = c2.carrier
then 'Correct carrier'
when s.carrier <> c2.carrier
then 'wrong carrier'
else 'No contract'
end as contractType
from Shipments s
left join Contracts c1
on s.route = c1.route
and s.carrier = c1.carrier
left join Contracts c2
on s.route = c2.route
where (
c1.route = c2.route
and c1.carrier = c2.carrier
)
or ( c1.route is null )
SELECT s.*,c.*
FROM Shipments s
LEFT JOIN Contracts c ON s.Route=c.Route and s.Carrier=c.Carrier;
如果 c.Route
为空,则这些货件没有合同。
你需要一个额外的连接来查看路由是否有契约然后CASE
逻辑:
SELECT s.*,
(CASE WHEN cr.route IS NULL THEN 'Route has no contract'
WHEN c.route IS NULL THEN 'Route assigned to wrong carrier'
ELSE 'Correct'
END)
FROM Shipments s LEFT JOIN
Contracts c
ON s.Route = c.Route AND s.carrier = c.carrier LEFT JOIN
(SELECT DISTINCT c.Route
FROM Contracts c
) cr
ON cr.Route = s.Route