Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I understand now. Your problem in the original query is that it is highly unusual (if not, in fact, wrong) to use a HAVING clause without a GROUP BY clause. The answer lies in the order of operation the various parts of the query are performed.</p> <p>In the original query, you do this:</p> <pre><code>SELECT COUNT(DISTINCT t442.c1) FROM ... WHERE ... HAVING COUNT(DISTINCT t631.c536870922) = COUNT(DISTINCT t649.c536870931); </code></pre> <p>The database will perform your joins and constraints, at which point it would do any group by and aggregation operations. In this case, you are not grouping, so the COUNT operations are across the whole data set. Based on the values you posted above, COUNT(DISTINCT t631.c536870922) = 25 and COUNT(DISTINCT t649.c536870931) = 24. The HAVING clause now gets applied, resulting in nothing matching - your asking for cases where the count of the total set (even though there are multiple c1s) are equal, and they are not. The DISTINCT gets applied to an empty result set, and you get nothing.</p> <p>What you really want to do is just a version of what you posted in the example that spit out the rows counts:</p> <pre><code>SELECT count(*) FROM (SELECT t442.c1 FROM t442 , t658 , t631 , t649 WHERE t442.c1 = t658.c536870930 AND t442.c200000003 = 'Network' AND t442.c536871139 &lt; 2 AND t631.c536870913 = t442.c1 AND t658.c536870925 = 1 AND ( t442.c7 = 6 OR t442.c7 = 5) AND t442.c536870954 &gt; 1141300800 AND (t442.c240000010 = 0) AND t442.c1 = t649.c536870914 AND t649.c536870939 &gt; 1 GROUP BY t442.c1 HAVING COUNT(DISTINCT t631.c536870922) = COUNT(DISTINCT t649.c536870931) ); </code></pre> <p>This will give you a list of the c1 columns that have equal numbers of the 631 &amp; 649 table entries. Note: You should be very careful about the use of DISTINCT in your queries. For example, in the case where you posted the results above, it is completely unnecessary; oftentimes it acts as a kind of wallpaper to cover over errors in queries that don't return results the way you want due to a missed constraint in the WHERE clause ("Hmm, my query is returning dupes for all these values. Well, a DISTINCT will fix that problem").</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