Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>the <code>"WHERE"</code> for <code>GROUP BY</code> is <code>HAVING</code>:</p> <pre><code>SELECT ipaddr from visitors GROUP BY ipaddr HAVING COUNT(ipaddr) = 1 AND MIN(date) &gt; '2011-03-31 00:59:11' </code></pre> <h2>UPDATE</h2> <pre><code>SELECT ipaddr, max(sid) sid FROM visitors GROUP BY ipaddr HAVING COUNT(DISTINCT sid) = 1 AND MIN(date) &gt; '2011-03-31 00:59:11' </code></pre> <p><strong>Explanation:</strong></p> <pre><code>SELECT date, sid, ipaddr FROM visitors date sid ipaddr ------------------------------------------ 2011-03-31 06:25:48 299525457 -1454342140 2011-03-31 06:26:37 299525457 -1454342140 2010-01-01 15:23:44 694388590 -1454342140 2011-03-31 11:23:44 111111111 -1234444811 2011-03-31 12:23:44 111111111 -1234444811 SELECT ipaddr FROM visitors GROUP BY ipaddr ipaddr ----------- -1454342140 -1234444811 --- group for ip -1454342140 --- 2011-03-31 06:25:48 299525457 -1454342140 2011-03-31 06:26:37 299525457 -1454342140 2010-01-01 15:23:44 694388590 -1454342140 COUNT(DISTINCT sid) = COUNT(299525457, 694388590) = 2 --&gt; there is more than 1 session for this ip: not good!!! ==&gt; group discarded --- group for ip -1234444811 --- 2011-03-31 11:23:44 111111111 -1234444811 2011-03-31 12:23:44 111111111 -1234444811 COUNT(DISTINCT sid) = COUNT(111111111) = 1 --&gt; OK (here COUNT(sid) = count(111111111, 111111111) = 2 --&gt; despite it is the same sid, the count is 2, that is why using DISTINCT) MIN(date) = '2011-03-31 11:23:44' &gt; '2011-03-31 00:59:11' --&gt; OK ==&gt; group accepted </code></pre> <p>Authorized columns in the <code>SELECT</code> are:</p> <ul> <li>columns used in the <code>GROUP BY</code> clause</li> <li>agregates of the other columns</li> </ul> <p><code>ipaddr</code> was used in GROUP BY but not <code>sid</code>. To have also <code>sid</code> I used MAX but remember that it will be applied only to the group of rows for current <code>ipaddr</code> and because of the conditions in the query there is 1 unique <code>sid</code> but repeated so the result will be that <code>sid</code></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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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