34,590
社区成员
发帖
与我相关
我的任务
分享
declare @B table (BH INT ,BM VARCHAR(8))
insert into @B
select 1001,'仓库1' union all
select 1002,'仓库2' union all
select 1001,'仓库2' union all
select 1003,'仓库3'
select BH,BM=(select top 1 BM from @B where BH=a.BH),SL
from @a a
/*
BH BM SL
----------- -------- -----
1001 仓库1 200
1002 仓库2 300
1003 仓库3 400
declare @a table (BH INT ,SL FLOAT)
insert into @A
select 1001,200 union all
select 1002,300 union all
select 1003,400
declare @B table (BH INT ,BM VARCHAR(8))
insert into @B
select 1001,'仓库1' union all
select 1002,'仓库2' union all
select 1001,'仓库2' union all
select 1003,'仓库3'
SELECT * FROM @a A LEFT JOIN
( SELECT BH,MIN(BM) AS bm
FROM @B GROUP BY BH
)
B ON A.BH = B.BH
-----------------------
1001 200 1001 仓库1
1002 300 1002 仓库2
1003 400 1003 仓库3
select
b.*,a.sl
from
a join b t
on
a.bu=b.bu
and
t.bh=(select max(bh) from tb where bm=t.bm)
select BH,BM=(select top 1 BM from tb2 where BH=a.BH),SL
from tb1 a