日期:2014-05-17  浏览次数:20423 次

急救啊,怎么在查询的时候,判断一个表a中3条(或多条)id相同的明细数据在表a中是否有相同的3条数据
求救啊,怎么在查询的时候,判断一个表a中3条(或多条)单据id相同的明细数据(商品id不同)在表a中是否有相同的3条数据

------解决方案--------------------

create table [a]([djbh] varchar(10),[spid] varchar(10),[shl] int)
insert [a]
select 'bh001' ,'sp001' ,10  union all
select 'bh001' ,'sp002' ,11  union all
select 'bh001' ,'sp003' ,15  union all
select 'bh002' ,'sp001' ,10  union all
select 'bh002' ,'sp002' ,11  union all
select 'bh002' ,'sp003' ,15  union all
select 'bh003' ,'sp001' ,97  union all
select 'bh003' ,'sp003' ,98  union all
select 'bh003' ,'sp004' ,99 
select * from a

djbh       spid       shl
---------- ---------- -----------
bh001      sp001      10
bh001      sp002      11
bh001      sp003      15
bh002      sp001      10
bh002      sp002      11
bh002      sp003      15
bh003      sp001      97
bh003      sp003      98
bh003      sp004      99

(9 行受影响)

--处理测试数据
select *,row_number() over(PARTITION BY djbh order by djbh,spid) as rowid into #a from a
select * from #a


djbh       spid       shl         rowid
---------- ---------- ----------- --------------------
bh001      sp001      10          1
bh001      sp002      11          2
bh001      sp003      15          3
bh002      sp001      10          1