Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL sort by count with condition, show history
    primarykey
    data
    text
    <p>Consider the following simple MySQL query:</p> <pre><code>SELECT date, count(*) cnt, ip FROM fwlog GROUP BY ip, date HAVING cnt&gt;100 ORDER BY date DESC, cnt DESC </code></pre> <p>It gives me something like:</p> <pre><code>date cnt src 2013-06-20 14441 172.16.a 2013-06-20 8887 172.16.b .... 2013-06-19 14606 172.16.b 2013-06-19 12455 172.16.a 2013-06-19 5205 172.16.c </code></pre> <p>That is, it's sorting the IPs by date, then by count, as directed.</p> <p>Now I would like the result to be:</p> <ul> <li>Show the IP with the highest count TODAY first,</li> <li>then show the counts for that IP for the last few days (independent of cnt),</li> <li>then show the IP with the second hightest count TODAY,</li> <li>then the history of that IP,</li> <li>etc.pp.</li> </ul> <p>Example of desired result:</p> <pre><code>date cnt src 2013-06-20 14441 172.16.a 2013-06-19 12455 172.16.a 2013-06-18 .... 172.16.a 2013-06-17 .... 172.16.a .... 2013-06-20 8887 172.16.b 2013-06-19 14606 172.16.b 2013-06-18 .... 172.16.b 2013-06-17 .... 172.16.b ... 2013-06-20 .... 172.16.c 2013-06-19 .... 172.16.c 2013-06-18 .... 172.16.c 2013-06-17 .... 172.16.c ... ... </code></pre> <p>Can this even be done using plain SQL?</p> <p>Bye,</p> <p>Marki</p> <p>==========================================</p> <p>@ Gordon Linoff:</p> <pre><code>SELECT datex, cnt, ip FROM fwlog WHERE ... GROUP BY ip , datex ORDER BY SUM(case when datex = DATE(NOW()) then 1 else 0 end) DESC , src, date DESC, cnt DESC 2013-06-20 47 10.11.y 2013-06-19 47 10.11.y 2013-06-18 45 10.11.y 2013-06-17 42 10.11.y 2013-06-16 14 10.11.y .... 2013-06-20 592 172.16.a 2013-06-19 910 172.16.a 2013-06-18 594 172.16.a 2013-06-17 586 172.16.a 2013-06-20 299 172.16.b </code></pre> <p>This is not quite right yet, the lower block should be at the top.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload