Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this:</p> <pre><code>select t.* from tablename t join ( select date from tablename where AtStore1='yes' group by date having count(*) &gt;= 4 ) ta on t.date = ta.date and t.AtStore1 = 'yes' union select * from tablename where AtStore2 = 'yes' </code></pre> <p>Result:</p> <pre><code>ID Name Size Date AtStore1 AtStore2 1 Apple Medium 2012-01-01 00:00:00.000 NULL Yes 2 Pear Medium 2011-12-31 00:00:00.000 Yes Yes 3 Lemon Small 2011-12-31 00:00:00.000 Yes Yes 4 Orange Small 2011-12-31 00:00:00.000 Yes NULL 5 Carrot Medium 2011-12-31 00:00:00.000 Yes NULL 6 Potato Small 2011-12-31 00:00:00.000 Yes NULL 7 Celery Large 2011-12-31 00:00:00.000 NULL Yes 12 Pear Medium 2011-12-30 00:00:00.000 Yes Yes 14 Orange Small 2011-12-30 00:00:00.000 NULL Yes 15 Carrot Medium 2011-12-30 00:00:00.000 NULL Yes 18 Onion Medium 2011-12-29 00:00:00.000 NULL Yes </code></pre> <p>In this case null values are <strong>intentionally</strong> eliminated by an aggregate.</p> <p>I'm not sure about <code>and t.AtStore1 = 'yes'</code> in <code>ta on t.date = ta.date and t.AtStore1 = 'yes'</code> remove it if you want all rows from specific day without caring whether it has <code>yes</code> value in AtStore1 column.</p> <p><strong>Added</strong>:</p> <p>Answer to question from comments, but this is quick and probably dirt way to do it (lack of time):</p> <pre><code>select t3.*, t4.qty from tablename t3 join ( select id, max(qty) as qty from ( select t.id, ta.qty from tablename t join ( select date, count(*) as qty from tablename where AtStore1='yes' group by date having count(*) &gt;= 4 ) ta on t.date = ta.date and t.AtStore1 = 'yes' union select id, 0 as src from tablename where AtStore2 = 'yes' ) t2 group by t2.id ) t4 on t3.id = t4.id </code></pre> <p>Result:</p> <pre><code>ID Name Size Date AtStore1 AtStore2 qty 1 Apple Medium 2012-01-01 00:00:00.000 NULL Yes 0 2 Pear Medium 2011-12-31 00:00:00.000 Yes Yes 5 3 Lemon Small 2011-12-31 00:00:00.000 Yes Yes 5 4 Orange Small 2011-12-31 00:00:00.000 Yes NULL 5 5 Carrot Medium 2011-12-31 00:00:00.000 Yes NULL 5 6 Potato Small 2011-12-31 00:00:00.000 Yes NULL 5 7 Celery Large 2011-12-31 00:00:00.000 NULL Yes 0 12 Pear Medium 2011-12-30 00:00:00.000 Yes Yes 0 14 Orange Small 2011-12-30 00:00:00.000 NULL Yes 0 15 Carrot Medium 2011-12-30 00:00:00.000 NULL Yes 0 18 Onion Medium 2011-12-29 00:00:00.000 NULL Yes 0 </code></pre>
    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.
 

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