Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Server 2005 -Join based on criteria in table column
    primarykey
    data
    text
    <p>Using SQL Server 2005, what is the most efficient way to join the two tables in the following scenario ?</p> <p>The number of records in each table could be fairly large about 200000 say.</p> <p>The only way I can currently think of doing this is with the use of cursors and some dynamic SQL for each item which will clearly be very inefficient.</p> <p>I have two tables - a <code>PERSON</code> table and a <code>SEARCHITEMS</code> table. The <code>SEARCHITEMS</code> table contains a column with some simple criteria which is to be used when matching records with the <code>PERSON</code> table. The criteria can reference any column in the <code>PERSON</code> table.</p> <p>For example given the following tables :</p> <p><code>PERSON</code> table</p> <pre><code>PERSONID FIRSTNAME LASTNAME GENDER AGE ... VARIOUS OTHER COLUMNS 1 Fred Bloggs M 16 .... 200000 Steve Smith M 18 </code></pre> <p><code>SEARCHITEMS</code> table</p> <pre><code>ITEMID DESCRIPTION SEARCHCRITERIA 1 Males GENDER = 'M' 2 Aged 16 AGE=16 3 Some Statistic {OTHERCOLUMN &gt;= SOMEVALUE AND OTHERCOLUMN &lt; SOMEVALUE} .... 200000 Males Aged 16 GENDER = 'M' AND AGE = 16 </code></pre> <p><code>RESULTS</code> table should contain something like this :</p> <pre><code>ITEMID DESCRIPTION PERSONID LASTNAME 1 Males 1 Bloggs 1 Males 200000 Smith 2 Aged 16 1 Bloggs .... 200000 Males Aged 16 1 Bloggs </code></pre> <p>It would be nice to be able to just do something like</p> <pre><code>INSERT INTO RESULTSTABLE SELECT * FROM PERSON P LEFT JOIN SEARCHITEMS SI ON (APPLY SI.SEARCHCRITERIA TO P) </code></pre> <p>But I can't see a way of making this work. Any help or ideas appreciated.</p>
    singulars
    1. This table or related slice is empty.
    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