查询重复记录中的一条数据(不是所有列都重复的)

xjtandqt 2007-12-01 11:10:17
我有个表数据如下:

装箱单号 箱号 订单编号 品名 色号 颜色 尺寸 件数
CP-200710-0001 10-10 0321 NULL 200 Black S 5
CP-200710-0001 10-10 0321 NULL 400 White S 5
CP-200710-0001 11-11 0321 NULL 400 White S 2
CP-200710-0001 11-11 0321 NULL 300 Red S 7
CP-200710-0001 07-09 0321 NULL 600 Orange S 10
CP-200710-0001 04-06 0321 NULL 200 Black S 10
CP-200710-0001 01-03 0321 NULL 200 Black S 10


想要得到的数据如下: 如果("箱号" 一样的话 取"件数" 大的那条记录) ,如果("箱号" 一样"件数"也一样那么随便取一条记录都可以)

装箱单号 箱号 订单编号 品名 色号 颜色 尺寸 件数
CP-200710-0001 10-10 0321 NULL 400 White S 5
CP-200710-0001 11-11 0321 NULL 300 Red S 7
CP-200710-0001 07-09 0321 NULL 600 Orange S 10
CP-200710-0001 04-06 0321 NULL 200 Black S 10
CP-200710-0001 01-03 0321 NULL 200 Black S 10

谢谢!
...全文
165 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
xjtandqt 2007-12-03
  • 打赏
  • 举报
回复
感谢大家的帮助!
不想使用临时表,所以就用“中国风_燃烧你的激情!!!”解决我的问题!
中国风 2007-12-01
  • 打赏
  • 举报
回复
借用楼上的数据:
--drop table #
create table #
( 装箱单号 varchar(100), 箱号 varchar(10), 订单编号 varchar(10), 品名 varchar(10), 色号 varchar(10), 颜色 varchar(10), 尺寸 varchar(10), 件数 int)
insert into #
select 'CP-200710-0001', '10-10', '0321' , NULL, '200', 'Black' , 'S' , 5 union all
select 'CP-200710-0001', '10-10', '0321' , NULL, '400', 'White' , 'S' , 5 union all
select 'CP-200710-0001', '11-11', '0321' , NULL, '400', 'White' , 'S' , 2 union all
select 'CP-200710-0001', '11-11', '0321' , NULL, '300', 'Red' , 'S' , 7 union all
select 'CP-200710-0001', '07-09', '0321' , NULL, '600', 'Orange' , 'S' , 10 union all
select 'CP-200710-0001', '04-06', '0321' , NULL, '200', 'Black' , 'S' , 10 union all
select 'CP-200710-0001', '01-03', '0321' , NULL, '200', 'Black' , 'S' , 10

go
select
*
from
# t
where
checksum(订单编号,品名,色号,颜色,尺寸,件数)=
(select top 1 checksum(订单编号,品名,色号,颜色,尺寸,件数)
from # where 装箱单号=t.装箱单号 and 箱号=t.箱号 order by 件数 desc )
/*
装箱单号 箱号 订单编号 品名 色号 颜色 尺寸 件数
---------------------------------------------------------------------------------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -----------
CP-200710-0001 10-10 0321 NULL 400 White S 5
CP-200710-0001 11-11 0321 NULL 300 Red S 7
CP-200710-0001 07-09 0321 NULL 600 Orange S 10
CP-200710-0001 04-06 0321 NULL 200 Black S 10
CP-200710-0001 01-03 0321 NULL 200 Black S 10

(所影响的行数为 5 行)


*/
中国风 2007-12-01
  • 打赏
  • 举报
回复
用:
checksum

binary_checksum
校验值列值
中国风 2007-12-01
  • 打赏
  • 举报
回复
select
*
from
ta t
where
checksum(订单编号,品名,色号,颜色,尺寸,件数)=
(select top 1 checksum(订单编号,品名,色号,颜色,尺寸,件数)
from ta where 装箱单号=t.装箱单号 and 箱号=t.箱号 order by 件数 desc )
fa_ge 2007-12-01
  • 打赏
  • 举报
回复

drop table #
create table #
( 装箱单号 varchar(100), 箱号 varchar(10), 订单编号 varchar(10), 品名 varchar(10), 色号 varchar(10), 颜色 varchar(10), 尺寸 varchar(10), 件数 int)
insert into #
select 'CP-200710-0001', '10-10', '0321' , NULL, '200', 'Black' , 'S' , 5 union all
select 'CP-200710-0001', '10-10', '0321' , NULL, '400', 'White' , 'S' , 5 union all
select 'CP-200710-0001', '11-11', '0321' , NULL, '400', 'White' , 'S' , 2 union all
select 'CP-200710-0001', '11-11', '0321' , NULL, '300', 'Red' , 'S' , 7 union all
select 'CP-200710-0001', '07-09', '0321' , NULL, '600', 'Orange' , 'S' , 10 union all
select 'CP-200710-0001', '04-06', '0321' , NULL, '200', 'Black' , 'S' , 10 union all
select 'CP-200710-0001', '01-03', '0321' , NULL, '200', 'Black' , 'S' , 10

--方法一
select id=identity(int,1,1),
*
into #t
from # a
where
not exists(select 1 from # where a.装箱单号=装箱单号 and 箱号=a.箱号 and 件数 >a.件数)
order by 箱号 desc




select * from #t a
where not exists(select 1 from #t where a.装箱单号=装箱单号 and 箱号=a.箱号 and id>a.id)


--方法二
select id=identity(int,1,1),* into #t from
(
select * from # a
where (select count(1) from # where a.装箱单号=装箱单号 and 箱号=a.箱号 and 件数 >a.件数 )<1
)a


select * from #t a
where not exists(select 1 from #t where a.装箱单号=装箱单号 and 箱号=a.箱号 and id>a.id)

/*
id 装箱单号 箱号 订单编号 品名 色号 颜色 尺寸 件数
----------- ---------------------------------------------------------------------------------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -----------
2 CP-200710-0001 10-10 0321 NULL 400 White S 5
3 CP-200710-0001 11-11 0321 NULL 300 Red S 7
4 CP-200710-0001 07-09 0321 NULL 600 Orange S 10
5 CP-200710-0001 04-06 0321 NULL 200 Black S 10
6 CP-200710-0001 01-03 0321 NULL 200 Black S 10

(所影响的行数为 5 行)

*/

xjtandqt 2007-12-01
  • 打赏
  • 举报
回复
这个表里,装箱单号+箱号+订单编号+品名+色号+尺寸 才是唯一的

xjtandqt 2007-12-01
  • 打赏
  • 举报
回复
我的表没错,

我的问题的,上面表是:原记录
下面表是: 我要的结果

fa_ge 2007-12-01
  • 打赏
  • 举报
回复
这个表里,箱号+订单编号+品名+色号+尺寸 才是唯一的

----------------------
如果是这样的话,楼主上面贴出的结果是错的,应譔是

装箱单号 箱号 订单编号 品名 色号 颜色 尺寸 件数
---------------------------------------------------------------------------------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -----------
CP-200710-0001 10-10 0321 NULL 200 Black S 5
CP-200710-0001 10-10 0321 NULL 400 White S 5
CP-200710-0001 11-11 0321 NULL 400 White S 2
CP-200710-0001 11-11 0321 NULL 300 Red S 7
CP-200710-0001 07-09 0321 NULL 600 Orange S 10
CP-200710-0001 04-06 0321 NULL 200 Black S 10
CP-200710-0001 01-03 0321 NULL 200 Black S 10

(所影响的行数为 7 行)

xjtandqt 2007-12-01
  • 打赏
  • 举报
回复
感谢楼上的帮忙
但你的答案还是不行

箱号为:10-10的记录有两条,我只要一条
晓风残月0110 2007-12-01
  • 打赏
  • 举报
回复

--取最大##
select a.* from t a where not exists(select 1 from T where a.装箱单号=装箱单号 and 箱号=a.箱号 and 件数 <a.件数)

--取最大
select a.* from t a where 件数=(select max(件数) from t where a.装箱单号=装箱单号 and 箱号=a.箱号 )


--按记录顺序取第一条
select a.* from t a where 件数=(select top 1 件数 from t where a.装箱单号=装箱单号 and 箱号=a.箱号 )

--取最小
select a.* from t a where 件数=(select min(件数) from t where a.装箱单号=装箱单号 and 箱号=a.箱号 )

--随机取
select a.* from t a where 件数=(select top 1 件数 from t where a.装箱单号=装箱单号 and 箱号=a.箱号 order by newid())


xjtandqt 2007-12-01
  • 打赏
  • 举报
回复
这个表里,箱号+订单编号+品名+色号+尺寸 才是唯一的
晓风残月0110 2007-12-01
  • 打赏
  • 举报
回复

--取最大##
select a.* from t a where not exists(select 1 from T where a.装箱单号=装箱单号 and 箱号=a.箱号 and 件数 <a.件数)

--取最大
select a.* from t a where d=(select max(件数) from t where a.装箱单号=装箱单号 and 箱号=a.箱号 )


--按记录顺序取第一条
select a.* from t a where d=(select top 1 件数 from t where a.装箱单号=装箱单号 and 箱号=a.箱号 )

--取最小
select a.* from t a where d=(select min(件数) from t where a.装箱单号=装箱单号 and 箱号=a.箱号 )

--随机取
select a.* from t a where d=(select top 1 件数 from t where a.装箱单号=装箱单号 and 箱号=a.箱号 order by newid())



fa_ge 2007-12-01
  • 打赏
  • 举报
回复
不能加distinct的,因为不完全相同
----------
同意,我刚看错了
xjtandqt 2007-12-01
  • 打赏
  • 举报
回复
5楼的不行
不可以拿色号来判断的
xjtandqt 2007-12-01
  • 打赏
  • 举报
回复
不能加distinct的,因为不完全相同
中国风 2007-12-01
  • 打赏
  • 举报
回复
where 装箱单号=t.装箱单号 and 箱号=t.箱号
and 订单编号=t.订单编号 and isnull(品名,'')=isnull(t.品名,'') and 尺寸=t.尺寸 and 件数=t.件数 and 色号>t.色号
--取相同的条件
fa_ge 2007-12-01
  • 打赏
  • 举报
回复
必须要加上distinct ,因为件数存在两个最大的情况
中国风 2007-12-01
  • 打赏
  • 举报
回复

select
*
from
ta t
where
not exists(select 1 from ta where 装箱单号=t.装箱单号 and 箱号=t.箱号
and 订单编号=t.订单编号 and isnull(品名,'')=isnull(t.品名,'') and 尺寸=t.尺寸 and 件数=t.件数 and 色号>t.色号)
fa_ge 2007-12-01
  • 打赏
  • 举报
回复

select distinct * from t a
where not exists(select 1 from t where a.装箱单号=装箱单号 and 箱号=a.箱号 and 件数 >a.件数 )
sunhonglei2004 2007-12-01
  • 打赏
  • 举报
回复
select a.* from tb a where 件数 = (select max(件数) from tb where 箱号 = a.箱号) order by a.箱号
加载更多回复(1)

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧