/* 请教一下,两表左连接后产生了一个新的表,表中记录A为多项, 对应记录B为一项,查询结果为表中出现多个重复的B记录, 想请教一下高手如何去除!在此表示十分感谢! */ if OBJECT_ID(N'table1',N'U') is not null drop table table1 go create table table1( id int not null, name varchar(50) null, ) goif OBJECT_ID(N'table2',N'U') is not null drop table table2 go create table table2( id int not null, Tname varchar(50) null, table1Id int not null ) goinsert into table1 select 1,'LXW' union all select 2,'LY' union all select 3,'T'insert into table2 select 1,'ass',1 select table1.*,table2.* from table1 left join table2 on table1.id=table2.table1Id where table2.table1Id is not null
select * from a where not exists(select 1 from b where 仓库编码=a.仓库编码 and 仓库名称=a.仓库名称)
--连接条件加以下限制select r select * from ta left join tb on --你的原来的连接条件 ,如下面: ta.name=tb.name --再加连接限制条件,还要是id最小的一条才可以 and ta.id=(select min(id) from ta as t where t.name=ta.name)
select 采购入库数量,出库数量 from ta left join tb on ta.仓库名称=tb.仓库名称
select b.id
from a join b on a.bid = b.id
group by b.id
having count(1) > 1
1 A库 测试产品 10 A库 A01000001 主轴 5
1 A库 测试产品 22 A库 A01000001 主轴 5
1 A库 测试产品 20 A库 A01000001 主轴 5
1 A库 测试产品 10 A库 A01000001 主轴 5
合 计 62 0
测试数据如上!出库产品中5为对应不到记录而产生的!
请教一下,两表左连接后产生了一个新的表,表中记录A为多项,
对应记录B为一项,查询结果为表中出现多个重复的B记录,
想请教一下高手如何去除!在此表示十分感谢!
*/
if OBJECT_ID(N'table1',N'U') is not null drop table table1
go
create table table1(
id int not null,
name varchar(50) null,
)
goif OBJECT_ID(N'table2',N'U') is not null drop table table2
go
create table table2(
id int not null,
Tname varchar(50) null,
table1Id int not null
)
goinsert into table1
select 1,'LXW' union all
select 2,'LY' union all
select 3,'T'insert into table2
select 1,'ass',1
select table1.*,table2.* from table1 left join
table2 on table1.id=table2.table1Id
where table2.table1Id is not null
1 A库 测试产品 22 A库 A01000001 主轴 5
1 A库 测试产品 20 A库 A01000001 主轴 5
1 A库 测试产品 10 A库 A01000001 主轴 5不是很明白,看到第一条和最后一条完全一样,可用distinct过滤后保留一条
select * from ta left join tb
on
--你的原来的连接条件 ,如下面:
ta.name=tb.name
--再加连接限制条件,还要是id最小的一条才可以
and ta.id=(select min(id) from ta as t where t.name=ta.name)
select 采购入库数量,出库数量 from ta left join tb on ta.仓库名称=tb.仓库名称
表A
仓库编码 仓库名称 存货名称 采购入库数量
1 A库 测试产品1 10
1 A库 测试产品2 22
1 A库 测试产品3 20
1 A库 测试产品4 10
表B
出库仓库名称 出库产品编码 出库产品名称 出库数量
A库 A01000001 主轴 5
A库 A01000002 主轴 5
A库 A01000003 主轴 5
A库 A01000004 主轴 5
你要查询存货名称的入库和出库情况use weimei
create table #A(仓库编码 int,仓库名称 varchar(10),存货名称 varchar(30),采购入库数量 int)
insert into #A
select 1,'A库','测试产品1',10 union all
select 1,'A库','测试产品2',22 union all
select 1,'A库','测试产品3',20 union all
select 1,'A库','测试产品4',10
create table #B(出库仓库名称 varchar(10), 出库产品编码 varchar(30), 出库产品名称 varchar(30), 出库数量 int)
insert #B
select 'A库', 'A01000001', '主轴', 5 union all
select 'A库', 'A01000002', '主轴', 5 union all
select 'A库', 'A01000003', '主轴', 5 union all
select 'A库', 'A01000004', '主轴', 5 select distinct 存货名称,采购入库数量,出库数量
from #A inner join #B on #A.仓库名称=#B.出库仓库名称
dbo.ntest1.入库单号, dbo.ntest1.存货编码, dbo.ntest1.存货名称,
dbo.ntest1.采购入库数量, dbo.ntest1.生产订单号, dbo.ntest2.出库仓库,
dbo.ntest2.出库仓库名称, dbo.ntest2.出库日期, dbo.ntest2.出库单号,
dbo.ntest2.出库产品编码, dbo.ntest2.出库产品名称, dbo.ntest2.出库数量,
dbo.ntest2.出库生产订单号
FROM dbo.ntest1 LEFT OUTER JOIN
dbo.ntest2 ON dbo.ntest1.存货编码 = dbo.ntest2.出库产品编码
WHERE (NOT EXISTS
(SELECT 1
FROM dbo.ntest2
具体是这样的
产品 数量 编码 数量
A库 测试产品 10 A库 A01000001 主轴 5
A库 测试产品 22 A库 A01000001 主轴 5
A库 测试产品 20 A库 A01000001 主轴 5
A库 测试产品 10 A库 A01000001 主轴 5
产品中在A表找到四条记录,其中在B表找到一条记录5,但是显示情况确实这样,与A表所对应的记录都被写成了5,我就是想把不应该写上5的记录改成空!
解释一下!
A库 测试产品 10(为数量) A库 A01000001 主轴 5(为系统所加入的)此数据应为空
A库 测试产品 22 (为数量) A库 A01000001 主轴 5 (为系统所加入的)此数据应为空
A库 测试产品 20 (为数量) A库 A01000001 主轴 5 (为系统所加入的)此数据应为空
A库 测试产品 10 (为数量) A库 A01000001 主轴 5 (此数据为正确数据) WHERE dbo.ntest2.出库产品编码 = dbo.ntest1.仓库编码))