22,210
社区成员
发帖
与我相关
我的任务
分享
--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 行)
*/
select
*
from
ta t
where
checksum(订单编号,品名,色号,颜色,尺寸,件数)=
(select top 1 checksum(订单编号,品名,色号,颜色,尺寸,件数)
from ta where 装箱单号=t.装箱单号 and 箱号=t.箱号 order by 件数 desc )
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 行)
*/
装箱单号 箱号 订单编号 品名 色号 颜色 尺寸 件数
---------------------------------------------------------------------------------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -----------
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 行)
--取最大##
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())
--取最大##
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())
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.色号)
select distinct * from t a
where not exists(select 1 from t where a.装箱单号=装箱单号 and 箱号=a.箱号 and 件数 >a.件数 )