Note that there are some explanatory texts on larger screens.

plurals
  1. POAll rows with at least # qualifying rows, per day
    primarykey
    data
    text
    <p>Say we have a table like this:</p> <pre><code>**tablename** ID Name Size Date AtStore1 AtStore2 1 Apple Medium 20120101 Yes 2 Pear Medium 20111231 Yes Yes 3 Lemon Small 20111231 Yes Yes 4 Orange Small 20111231 Yes 5 Carrot Medium 20111231 Yes 6 Potato Small 20111231 Yes 7 Celery Large 20111231 Yes 8 Onion Medium 20111231 9 Tomato Medium 20111231 10 Apple Medium 20111231 12 Pear Medium 20111230 Yes Yes 13 Lemon Small 20111230 Yes 14 Orange Small 20111230 Yes 15 Carrot Medium 20111230 Yes 16 Potato Small 20111230 17 Celery Large 20111229 18 Onion Medium 20111229 Yes 19 Tomato Medium 20111229 </code></pre> <p>Can we construct an efficient query that retrieves ALL data per row where:</p> <ul> <li>"Yes" AtStore1 rows where there are at least 4 "Yes"es in AtStore1 on that day single day<br> OR (inclusive)</li> <li>There is a "Yes" in AtStore2</li> </ul> <p>Also acceptable is if only the first parameter is satisfied, i.e. the forward lookup of rows. I can probably script the AtStore2 part with PHP if necessary. All of my attempts have failed miserably; my attempts on how to write the question effectively in Google weren't fruitful either.</p> <p>(Answers breaking things out into different parts of code in PHP are fine too, I just want something reasonably efficient.)</p> <p>For this example table, these would be the expected rows:</p> <pre><code>ID Name Size Date AtStore1 AtStore2 1 Apple Medium 20120101 Yes 2 Pear Medium 20111231 Yes Yes 3 Lemon Small 20111231 Yes Yes 4 Orange Small 20111231 Yes 5 Carrot Medium 20111231 Yes 6 Potato Small 20111231 Yes 7 Celery Large 20111231 Yes 12 Pear Medium 20111230 Yes Yes 14 Orange Small 20111230 Yes 15 Carrot Medium 20111230 Yes 18 Onion Medium 20111229 Yes </code></pre> <p>As you can see</p> <ul> <li>Jan 1 2012<br> -No Yeses in AtStore1<br> -AtStore2 has a Yes so the row is returned</li> <li>Dec 31<br> -5 Yeses in AtStore1, so those are returned (AtStore2 Yeses in ID 2 and 3 would have sufficed as well)<br> -Also has ID 7 since there is a Yes in AtStore2</li></li> <li>The rest of the rows returned were only because of Yeses in AtStore2</li> </ul>
    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