关于左连接的问题,详情请进,有点难度。
表t1,有列: 供应商 货号 生产数量
A 001 200
B 001 30
B 002 500
B 003 100
表t2 有列: 货号 订货数量
001 500
002 700
希望左连接成如下的形式:(t1 left join t2 on t1.货号=t2.货号)
供应商 货号 订货数量 生产数量
A 001 500 200
B 001 - 30 (正常情况下,本行的订货数量也会被连接成500)
B 002 700 500
B 003 100
怎样去掉那行的数量置为null
[解决办法]
明白了,按gahade(与君共勉) ( ) 的吧
-----
select 供应商,
a.货号,
case when 生产数量=(select max(生产数量) from t1 where a.货号=t1.货号) then 订货数量 else NULL end as '订货数量 ',
生产数量
from t1 a
left join t2 on a.货号=t2.货号
[解决办法]
告诉大鸟们去掉的条件是什么
[解决办法]
那就需要一个排序字段
drop table t1,t2
go
create table t1(供应商 varchar(10),货号 varchar(10),生产数量 int)
insert into t1
select 'A ', '001 ',200
union all select 'B ', '001 ',30
union all select 'C ', '001 ',200
union all select 'B ', '002 ',500
union all select 'B ', '003 ',100
create table t2(货号 varchar(10),订货数量 int)
insert into t2
select '001 ',500
union all select '002 ',700
alter table t1 add id int identity(1,1)
select 供应商,
a.货号,
case when id=(select min(id) from t1 where a.货号=t1.货号) then 订货数量 else NULL end as '订货数量 ',
生产数量
from t1 a
left join t2 on a.货号=t2.货号
/*
供应商 货号 订货数量 生产数量
---------- ---------- ----------- -----------
A 001 500 200
B 001 NULL 30
C 001 NULL 200
B 002 700 500
B 003 NULL 100
(所影响的行数为 5 行)
*/
[解决办法]
create table t1(供应商 varchar(10),货号 varchar(10), 生产数量 int)
create table t2(货号 varchar(10),订货数量 int)
insert into t1 select 'A ' , '001 ' , 200 union all
select 'B ' , '001 ' , 30 union all
select 'B ' , '002 ' , 500 union all
select 'B ' , '003 ' , 100
insert into t2 select '001 ' , 500 union all
select '002 ' , 700
select 供应商,t1.货号,订货数量,生产数量 from t1 left join t2 on t1.货号=t2.货号
[解决办法]
那为什么不是这样?
供应商 货号 订货数量 生产数量
A 001 - 200
B 001 500 30
B 002 700 500
B 003 100
也就是你的分配策略是怎样的?或者说你的供应商的优先级别在哪里体现?