Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove dupes from recordset excluding columns from dupe condition
    primarykey
    data
    text
    <p>I'm up against a mssql database, having a SQL query like...</p> <pre><code>SELECT id, type, start, stop, one, two, three, four FROM a UNION ALL SELECT id, type, start, stop, one, two, three, four FROM b UNION ALL SELECT id, type, start, stop, one, two, three, four FROM c ORDER BY type ASC </code></pre> <p>Resulting in...</p> <pre><code>row | id type start stop one two three four ----+-------------------------------------------------------------- 1 | 1 a 2010-01-01 2010-01-31 100 1000 1000 100 2 | 1 a 2010-02-01 2010-12-31 100 500 500 50 3 | 1 b 2010-01-01 2010-01-31 100 NULL NULL 100 4 | 1 b 2010-01-01 2010-12-31 100 NULL NULL 100 5 | 1 c 2010-01-01 2010-01-31 0 NULL NULL 100 6 | 1 c 2010-01-01 2010-12-31 0 NULL NULL 100 </code></pre> <p>However, I would much rather prefer the following outcome...</p> <pre><code>row | id type start stop one two three four ----+-------------------------------------------------------------- 1 | 1 a 2010-01-01 2010-01-31 100 1000 1000 100 2 | 1 a 2010-02-01 2010-12-31 100 500 500 50 4 | 1 b 2010-01-01 2010-12-31 100 NULL NULL 100 6 | 1 c 2010-01-01 2010-12-31 0 NULL NULL 100 </code></pre> <p>That is, eliminating row 3 and 5, since they are dupes to row 4 and 6 in every way <strong>but</strong> the <em>stop</em>-column, AND whereas the unfortunate row having to lowest value in the excluding <em>stop</em>-column is to be removed.</p> <p>How can I accomplish this? I've been thinking something like...</p> <pre><code>SELECT * FROM ( SELECT id, type, start, stop, one, two, three, four FROM a UNION ALL SELECT id, type, start, stop, one, two, three, four FROM b UNION ALL SELECT id, type, start, stop, one, two, three, four FROM c ORDER BY type ASC ) AS types GROUP BY ... HAVING ??? </code></pre> <p>I need guidance, please help.</p> <p>(And no, I'm in no position to change any conditions, I've got to work with the given situation.)</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. 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