34,594
社区成员
发帖
与我相关
我的任务
分享
create table T(ID int)
insert T select 1
insert T select 2
insert T select 3
go
create table T2(ID int,num int)
insert T2 select 1,2
insert T2 select 1,3
insert T2 select 2,4
insert T2 select 2,5
select
t.ID,
num=isnull(rtrim(t2.num),''),
row=identity(int,1,1)
into #
from
t
left join
t2 on t.ID=t2.ID
select
[ID]=case when row=(select min(row) from # where ID=a.ID) then rtrim(ID) else '' end,
num
from
# a
ID num
------------ ------------
1 2
3
2 4
5
3
(所影响的行数为 5 行)
select a.编号,b.货物名称,sum(件数),sum(毛重),sum(体积)
from a left join b on b.编号 = a.编号
group by a.编号,b.货物名称