I have a very simple table (LOG) containing attributes MAC_ADDR, IP_SRC, IP_DST, URL, PROTOCOL. I want the first n lines containing IP_SRC, URL, #OfOccurrences ordered by decreasing #OfOccurrences for each IP_SRC in my table when PROTOCOL='DNS'.
To be clearer, I want to be able to list the first n most visited pages for each IP_SRC in my table.
I can get the most visited URL for each IP_SRC like this :
select ip_src,url,cnt
from (
select ip_src,url,count(*) as cnt,protocol
from log as b group by ip_src,url order by ip_src,cnt desc
) as c
where cnt>=(select MAX(cpt)
from (select count(*) as cpt from log as b
where c.ip_src==b.ip_src group by ip_src,url)
)
and protocol='DNS';
However, this solution is obviously not optimized.
Here is a more practical code (for the most visited URL for each IP_SRC) :
select ip_src,url,cnt
from (select ip_src,url,count(*) as cnt
from log where protocol='DNS'
group by ip_src,url
order by ip_src,cnt asc)
group by ip_src;
This second option is way more faster ! However, I want the n most visited pages for each IP_SRC, and I can't figure out how to do.
Thanks for your help.