mysql查询重复数和输出格式
我有我的数据的这种表示,其中col1是recID(int),col2是version(varchar),col3是patch(int) 本表中有重复项(rec 36,37): 我使用此查询为我提供DUP:mysql查询重复数和输出格式,mysql,sql,Mysql,Sql,我有我的数据的这种表示,其中col1是recID(int),col2是version(varchar),col3是patch(int) 本表中有重复项(rec 36,37): 我使用此查询为我提供DUP: select recID, count(Version) as Count, Version from Test group by Version having count > 1; 输出: recID Count Version Patch 32 3
select recID,
count(Version) as Count,
Version
from Test
group by Version
having count > 1;
输出:
recID Count Version Patch
32 3 434 -
如何修复此查询?我想列出recID,这是错误的,因为recID不是dup,但有空值
但是我想要的是给我DUP的版本,并以这种方式列出(如果可能的话)
预期产出:
recID Count Version Patch
32 1 5.1.4 434 -
36 2 5.1.4 434 p2
37 - 5.1.4 434 p2
您可以使用变量获得预期的输出
select recID, version, patch,
case when @prevValue = version
then @cnt:=@cnt+1
else @cnt:=0
end as DupCount,
@prevValue:=version as var_version
FROM Table1, ( select @prevValue := NULL , @cnt:=0 ) t
order by version
您可以使用下面的查询,它将给出您需要的结果
select t1.recID,t1.Version,t1.Patch,t2.cnt,
case when exists(select * from #tmp where t1.recID > #tmp.recID and t1.Patch=#tmp.Patch and t1.Version=#tmp.Version ) then 0 else t2.cnt end as cnt2
from #tmp t1 inner join (
select Version,Patch ,COUNT(recId) as cnt from #tmp
group by Version,Patch)t2
on t1.Patch=t2.Patch and t1.Version=t2.Version
谢谢你的帮助。我的计数似乎不准确。对于recID=37,我得到的dup计数为2,对于recID=36,得到的dup计数为3,对于recID=34,得到的dup计数为4
select t1.recID,t1.Version,t1.Patch,t2.cnt,
case when exists(select * from #tmp where t1.recID > #tmp.recID and t1.Patch=#tmp.Patch and t1.Version=#tmp.Version ) then 0 else t2.cnt end as cnt2
from #tmp t1 inner join (
select Version,Patch ,COUNT(recId) as cnt from #tmp
group by Version,Patch)t2
on t1.Patch=t2.Patch and t1.Version=t2.Version