Note that there are some explanatory texts on larger screens.

plurals
  1. POGiven desired results and database information, programically build an SQL query that gives those results
    text
    copied!<p>I don't think there's an easy way to do this, but on the off chance that there is ...</p> <p>I am given a number of lists of around 10000 records each from a 10 million record table. The data is currently generated by queries on various non-indexed elements. I want to automatically build queries that give the same results, using ten separate indexed fields. </p> <p>Is there a known algorithm for building something like this? Beyond the basics of including each indexed 'node' with its own OR, I mean. </p> <p>E.g., assuming the data wanted is:</p> <pre><code>Letter, Number A, 1 A, 2 B, 1 C, 2 </code></pre> <p>and the original database has</p> <pre><code>Letter, Number A, 1 A, 2 A, 3 B, 1 C, 1 C, 2 D, 1 D, 3 </code></pre> <p>I'd like something like:</p> <pre><code>WHERE ((Letter = 'A' OR Letter = 'B') AND (Number = 1 OR Number = 2)) OR (Letter = 'C' and Number = 2) </code></pre> <p>Or maybe</p> <pre><code>WHERE (Letter IN ('A', 'B', 'C') AND Number IN (1, 2) AND NOT (Number = 1 AND Letter = 'C')) </code></pre> <p>But I <em>think</em> I'd rather not have </p> <pre><code>WHERE (Letter = 'A' AND Number = '1') OR (Letter = 'A' AND Number = '2') OR (Letter = 'B' AND Number = '1') OR (Letter = 'C' AND Number = '2') </code></pre> <p>-- unless the database experts here think that that would be much more optimized in the long term, for the sample size we're talking about. Run time of the queries is important; run time of the conversion tool is not. I also don't need to necessarily get the 'best' answer; 'good enough' is acceptable.</p> <p>My current plan is to count, sort and iterate through looking for things that can be grouped together, to try to make as few 'groupings' as possible; I think I'd rather not have ten thousand (A and B and C and D and E and F and G and H and I and J)'s ORed together. </p> <p>Thoughts? Expert advice?</p>
 

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