求一条SQL语句

xiaoxinpaigu 2010-12-11 10:12:10
task class barcode
101 b D2010001
102 b D2010002
101 w D2010003
101 b D2010004
102 w D2010005
102 w D2010005


task = 订单号
class = 班次(白班b晚班w)
barcode = 条码

以上表数据,我要找出同一个订单号相同班次出现两个以上不同条码的结果的订单号,最终找到101

...全文
71 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
DataBox-MDX 2010-12-11
  • 打赏
  • 举报
回复

--task class barcode
--101 b D2010001
--102 b D2010002
--101 w D2010003
--101 b D2010004
--102 w D2010005
--102 w D2010005
use City;
go
if OBJECT_ID(N'A',N'U') is not null drop table A
go
create table A
(
--id int identity(1,1) primary key not null,
task int,
class nvarchar(5),
barcode nvarchar(20)
)
go
insert into A
select 101, 'b','D2010001' union all
select 102, 'b','D2010002' union all
select 101 ,'w','D2010003' union all
select 101 ,'b','D2010004' union all
select 102 ,'w','D2010005' union all
select 102 ,'w','D2010005'
go
with cte as
(
select task,
ROW_NUMBER()over(partition by task,class order by barcode) as RN
from (select distinct * from A)as D
)
select task from cte where RN>=2
/*
(6 行受影响)
task
-----------
101

(1 行受影响)
*/


王向飞 2010-12-11
  • 打赏
  • 举报
回复
--> 测试数据:#tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
create table #tb([task] int,[class] varchar(1),[barcode] varchar(8))
insert #tb
select 101,'b','D2010001' union all
select 102,'b','D2010002' union all
select 101,'w','D2010003' union all
select 101,'b','D2010004' union all
select 102,'w','D2010005' union all
select 102,'w','D2010005'

select task,class,COUNT(distinct barcode) from #tb group by task,class
having COUNT(distinct barcode)>1
百年树人 2010-12-11
  • 打赏
  • 举报
回复
select task
from tb
group by task,class
having count(distinct barcode)>1
rucypli 2010-12-11
  • 打赏
  • 举报
回复
select distinct task
from tb A
where exists (select 1 from tb B where A.task=B.task and A.class=B.class and A.barcode<>b.barcode)

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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