22,302
社区成员




select * from tb t where
(select count(1) from tb where 单别=t.单别 and 单号=t.单号 and 总序号=t.总序号) > 1
select max(序号) from tb group by 单别,单号 having count(序号)>1
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(序号 int, 单别 varchar(8), 单号 int, 合同号 varchar(10), 总量 numeric(5,2), 总序号 varchar(27), 批号 int)
insert into #
select 1, '51S2', 100831137, '1100752-31', 131.82, '221.1100752-31.0001.001.014', '20100831' union all
select 2, '51S2', 100831138, '1100752-31', 131.82, '221.1100752-31.0001.003.022', '20100831' union all
select 3, '51S2', 100831137, '1100752-31', 131.82, '221.1100752-31.0001.001.015', '20100831' union all
select 4, '51S2', 100831137, '1100752-31', 131.82, '221.1100752-31.0001.001.015', '20100831' union all
select 5, '51S2', 100831137, '1100752-31', 131.82, '221.1100752-31.0001.002.002', '20100831' union all
select 6, '51S2', 100831137, '1100752-31', 131.82, '221.1100752-31.0001.002.003', '20100831' union all
select 7, '51S2', 100831137, '1100752-31', 131.82, '221.1100752-31.0001.002.004', '20100831' union all
select 8, '51S2', 100831137, '1100752-31', 131.82, '221.1100752-31.0001.002.005', '20100831' union all
select 9, '51S2', 100831138, '1100752-31', 131.82, '221.1100752-31.0001.003.023', '20100831' union all
select 10, '51S2', 100831138, '1100752-31', 131.82, '221.1100752-31.0001.003.023', '20100831' union all
select 11, '51S5', 100831138, '1100752-31', 131.82, '221.1100752-31.0001.003.023', '20100831' union all
select 12, '51S2', 100831137, '1100752-31', 131.82, '221.1100752-31.0001.002.021', '20100831' union all
select 13, '51S2', 100831136, '1100752-31', 131.82, '221.1100752-31.0001.001.001', '20100831' union all
select 14, '51S2', 100831136, '1100752-31', 131.82, '221.1100752-31.0001.001.001', '20100831' union all
select 15, '51S2', 100831136, '1100752-31', 131.82, '221.1100752-31.0001.001.003', '20100831' union all
select 16, '51S2', 100831136, '1100752-31', 131.82, '221.1100752-31.0001.001.004', '20100831' union all
select 17, '51S2', 100831136, '1100752-31', 131.82, '221.1100752-31.0001.001.005', '20100831' union all
select 18, '51S2', 100831136, '1100752-31', 131.82, '221.1100752-31.0001.001.006', '20100831'
select * from # t where
exists (select 1 from # where 单别=t.单别 and 单号=t.单号 and 总序号=t.总序号 and 序号<>t.序号)
/*
序号 单别 单号 合同号 总量 总序号 批号
----------- -------- ----------- ---------- ------ --------------------------- -----------
3 51S2 100831137 1100752-31 131.82 221.1100752-31.0001.001.015 20100831
4 51S2 100831137 1100752-31 131.82 221.1100752-31.0001.001.015 20100831
9 51S2 100831138 1100752-31 131.82 221.1100752-31.0001.003.023 20100831
10 51S2 100831138 1100752-31 131.82 221.1100752-31.0001.003.023 20100831
13 51S2 100831136 1100752-31 131.82 221.1100752-31.0001.001.001 20100831
14 51S2 100831136 1100752-31 131.82 221.1100752-31.0001.001.001 20100831
*/
select * from tb t where exists (select 1 from tb where 单别=t.单别 and 单号=t.单号 and 总序号=t.总序号 and 序号<>t.序号)
select a.序号 as 序号1,b.序号 as 序号2 from tb a inner join tb b on a.单别=b.单别 and a.单号=b.单号 and a.总序号=b.总序号 and a.序号<>b.序号