If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > How to create a GROUP BY for use with ip address range?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-14-07, 07:31
Shyckymn Shyckymn is offline
Registered User
 
Join Date: Mar 2007
Posts: 11
Question How to create a GROUP BY for use with ip address range?

Dudes,

How can I make a GROUP BY function into an SQL instruction, to count how much machines by ip address range (until the 3rd range)?

Ex: (Existent registers for a field called ip_addr)

172.17.17.1
172.17.17.2
172.17.17.3
172.18.196.12
172.18.196.22
172.22.3.1

Must returns:
Network 172.17.17: 3 machines
Network 172.18.196: 2 machines
Network 172.22.3: 1 machine
Reply With Quote
  #2 (permalink)  
Old 03-14-07, 07:49
Shyckymn Shyckymn is offline
Registered User
 
Join Date: Mar 2007
Posts: 11
Question How to create a GROUP BY for use with ip address range?

Dudes,

How can I make a GROUP BY function into an SQL instruction, to count how much machines by ip address range (until the 3rd range)?

Ex: (Existent registers for a field called ip_addr)

172.17.17.1
172.17.17.2
172.17.17.3
172.18.196.12
172.18.196.22
172.22.3.1

Must returns:
Network 172.17.17: 3 machines
Network 172.18.196: 2 machines
Network 172.22.3: 1 machine
Reply With Quote
  #3 (permalink)  
Old 03-14-07, 07:59
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You write a UDF that takes the whole IP address column and returns just the first 3 ranges. Then you query looks like: SELECT MyUDF(IPCOL),count(*) from MYTable group by MyUDF(IPCOL) ORDER BY 1

HTH

Andy
Reply With Quote
  #4 (permalink)  
Old 03-14-07, 08:00
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Answered in duplicate thread.
Reply With Quote
  #5 (permalink)  
Old 03-14-07, 08:30
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
locate 3 location of '.'

select loc,count(*) from (select (substr(IP,1,locate('.',IP,(locate('.',IP,((locate ('.',IP))+1))+1)))) As loc from Test11 )As B
group by loc

LOC 2
--------------- -----------
172.17.17. 3
172.17.172. 1
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #6 (permalink)  
Old 03-15-07, 04:21
vini_srcna vini_srcna is offline
Registered User
 
Join Date: May 2006
Posts: 82
I couldn't test this query. Would this work for you..?

SELECT SUBSTR(LOC,1,LOCATE('.',LOC,3)),COUNT(*) from tablename GROUP BY SUBSTR(LOC,1,LOCATE('.',LOC,3))
__________________
Vinay,
Reply With Quote
  #7 (permalink)  
Old 03-15-07, 08:23
rahul_s80 rahul_s80 is offline
Registered User
 
Join Date: Jul 2006
Location: Pune , India
Posts: 433
3 parameter in locate specifies the starting position from where to start search

values locate('r','rahulrrsingh',2)

1
-----------
6

values locate('r','rahulrrsingh',3)

1
-----------
6
__________________
Rahul Singh
Certified DB2 9 DBA / Application Developer
Reply With Quote
  #8 (permalink)  
Old 03-15-07, 15:59
Shyckymn Shyckymn is offline
Registered User
 
Join Date: Mar 2007
Posts: 11
"vini_srcna", sorry, but it doesn't work...

Result:

0.0. 2
0.1. 1
1.1. 3
10. 500
169. 11
172. 4312
192. 195
200. 16
Reply With Quote
  #9 (permalink)  
Old 03-15-07, 16:02
Shyckymn Shyckymn is offline
Registered User
 
Join Date: Mar 2007
Posts: 11
"rahul_s80", your tip was simply perfect!

Thanks a lot, dude!
Reply With Quote
  #10 (permalink)  
Old 03-16-07, 03:47
vini_srcna vini_srcna is offline
Registered User
 
Join Date: May 2006
Posts: 82
Thats great. Thanks for the update.
__________________
Vinay,
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On