这样写呢?select * from 订单表 a inner join 箱唛表 b on (a.目的国= b.目的国 and a.订单尾号=b.订单尾号 and b.箱唛规则=【目的国+订单尾号】) or (a.目的国= b.目的国 and a.订单尾号<>b.订单尾号 and a.运输方式=b.运输方式 and b.箱唛规则=【目的国+运输方式】)
那么两种改法,简单该法是别抽箱唛规则字段,然后distinct去重复 复杂点的是对你抽出来的结果集以订单编号分组做rownumber over partition by,然后取rownumber为1的一条数据
--看你需求,是不能用 Or 的,这么写试试,union all 对应的字段不能用*,用啥你自己调节 with t as ( select * ,1 sort from 订单表 a inner join 箱唛表 b on (a.目的国= b.目的国 and a.订单尾号=b.订单尾号 and b.箱唛规则=【目的国+订单尾号】) union all select * ,2 sort from 订单表 a inner join 箱唛表 b on (a.目的国= b.目的国 and a.运输方式=b.运输方式 and b.箱唛规则=【目的国+运输方式】) ),tt as ( select *,rn=ROW_NUMBER(partition by a.订单编号 order by sort) from t ) select * from tt where tt.rn=1
inner join 箱唛表 b on
(a.目的国= b.目的国 and a.订单尾号=b.订单尾号 and b.箱唛规则=【目的国+订单尾号】)
or
(a.目的国= b.目的国 and a.订单尾号<>b.订单尾号 and a.运输方式=b.运输方式 and b.箱唛规则=【目的国+运输方式】)
复杂点的是对你抽出来的结果集以订单编号分组做rownumber over partition by,然后取rownumber为1的一条数据
with t as (
select * ,1 sort
from 订单表 a
inner join 箱唛表 b on (a.目的国= b.目的国 and a.订单尾号=b.订单尾号 and b.箱唛规则=【目的国+订单尾号】)
union all
select * ,2 sort
from 订单表 a
inner join 箱唛表 b on (a.目的国= b.目的国 and a.运输方式=b.运输方式 and b.箱唛规则=【目的国+运输方式】)
),tt as (
select *,rn=ROW_NUMBER(partition by a.订单编号 order by sort)
from t
)
select * from tt where tt.rn=1