急救啊,怎么在查询的时候,判断一个表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
--处理测试数据
select *,row_number() over(PARTITION BY djbh order by djbh,spid) as rowid into #a from a
select * from #a
--按单据编号进行分组合并
SELECT [djbh],
(SELECT [spid]+',' FROM #a WHERE djbh=a.djbh
FOR XML PATH('')
) AS [spid] ,
(SELECT cast([shl] as varchar(10))+',' FROM #a WHERE djbh=a.djbh
FOR XML PATH('')
) AS [shl]
into #b
FROM #a a
GROUP BY [djbh]
select * from #b
--查询重复数量大于2的单据号
select djbh from
#b a,
(select spid,shl from #b group by spid,shl having count(djbh)>=2) b
where a.spid=b.spid and a.shl=b.shl