Get count of rows having values in ascending sequence like 1,2,3 until a break occurs like 1,2,3,7,8,9,10 from table

ip_available Table data is:

ip_addressparent
100100100241987
100100100261987
100100100271987
100100100331987
100100100341987
100100100351987
100100100361987
100100100371987
100100100902010

Similarly there are more ip_addresses with other parent values

I want result set like

ip_address_minparentcount
1001001002419873
1001001003319875
1001001009020101

DB is MySQL
Count should come for each regular sequence, No matter if parent value is repeated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

A little more details would help out next time, like including your table structure.
But I believe this will help you out.

SELECT MIN(ip_address) as ip_address_min, MIN(parent) as parent, COUNT(ip_address) as count 
FROM ip_available
GROUP BY LEFT(ip_address,length(ip_address)-1);

https://www.db-fiddle.com/f/5dw4hvSfLm5idNjknrj6YX/0


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x