select - how to find duplicates and gaps in this scenario in mysql -
hi have table looks like
----------------------------------------------------------- | id | group_id | source_id | target_id | sortsequence | ----------------------------------------------------------- | 2 | 1 | 2 | 4 | 1 | ----------------------------------------------------------- | 4 | 1 | 20 | 2 | 1 | ----------------------------------------------------------- | 5 | 1 | 2 | 14 | 1 | ----------------------------------------------------------- | 7 | 1 | 2 | 7 | 3 | ----------------------------------------------------------- | 20 | 2 | 20 | 4 | 3 | ----------------------------------------------------------- | 21 | 2 | 20 | 4 | 1 | -----------------------------------------------------------
scenario
there 2 scenarios needs handled.
sortsequence
column value should unique against 1source_id
,group_id
. example if records havinggroup_id = 1 , source_id = 2
should have sortsequence unique. in above example records havingid= , 5 having group_id = 1 , source_id = 2 have same sortsequence 1
. faulty record. need find out these records.- if
group_id , source_id
same.sortsequence columns value should continous. there should no gap
. example in above tablerecords having id = 20, 21 having same group_id , source_id , sortsequence value 3 , 1
. unique there gap in sortsequence value. need find out these records.
my far effort
i have written query
select source_id,`group_id`,group_concat(id) children table group source_id, sortsequence, `group_id` having count(*) > 1
this query address scenario 1. how handle scenario 2? there way in same query or have write other handle second scenario.
by way query dealing million of records in table performance must good.
got answer tere j
comments. following query covers above mentioned both criteria.
select source_id, `group_id`, group_concat(id) faultyids table group source_id,group_id having count(distinct sortsequence) <> count(sortsequence) or count(sortsequence) <> max(sortsequence) or min(sortsequence) <> 1
may can others.
Comments
Post a Comment