<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:admin="http://webns.net/mvcb/"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
	<channel> 

      <title>Comments on: query criteria from a table?</title>
      <link>http://ask.metafilter.com/50261/query-criteria-from-a-table/</link>
      <description>Comments on Ask MetaFilter post query criteria from a table?</description>
	  	  <pubDate>Mon, 06 Nov 2006 06:13:46 -0800</pubDate>
      <lastBuildDate>Mon, 06 Nov 2006 06:13:46 -0800</lastBuildDate>
      <language>en-us</language>
	  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
	  <ttl>60</ttl>

<item>
  	<title>Question: query criteria from a table?</title>
  	<link>http://ask.metafilter.com/50261/query-criteria-from-a-table</link>	
  	<description>Access SQL:
How to use a short list of numbers stored as text in a table cell  as the criteria for a query? &lt;br /&gt;&lt;br /&gt; I&apos;m trying to insert a list of values from a table into the where clause of a query.  I have a several sets of values stored as text that I&apos;d like to use as criteria in a query, for example, &lt;br&gt;
&lt;br&gt;
Table:&lt;br&gt;
ID    CoumnA&lt;br&gt;
1  |  &quot;3&quot;, &quot;5&quot;,&quot;17&quot;, &quot;34&quot;&lt;br&gt;
2  |  &quot;1&quot;, &quot;12&quot;,&quot;15&quot;&lt;br&gt;
&lt;br&gt;
Where clauses:&lt;br&gt;
&lt;br&gt;
WHERE ColumnA IN( &quot;3&quot;, &quot;5&quot;,&quot;17&quot;, &quot;34&quot;)&lt;br&gt;
or,&lt;br&gt;
WHERE ColumnA IN( &quot;1&quot;, &quot;12&quot;,&quot;15&quot;)&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Is there an easy way to get those values from a single cell in a table and into the query?  I&apos;ve tried a few things, DLookUp, for example, but get no values returned.&lt;br&gt;
I think there should be a quick way of doing this without resorting to VBA, but I can&apos;t  think what it is.</description>
  	<guid isPermaLink="false">post:ask.metafilter.com,2008:site.50261</guid>
  	<pubDate>Mon, 06 Nov 2006 05:15:43 -0800</pubDate>
  	<dc:creator>Zetetics</dc:creator>
	
	<category>Access</category>
	
	<category>SQL</category>
	
</item>
<item>
  	<title>By: derbs</title>
  	<link>http://ask.metafilter.com/50261/query-criteria-from-a-table#762513</link>	
  	<description>WHERE LIKE ColumnA=&apos;%&amp;quot;34&amp;quot;%&apos;&lt;br&gt;
WHERE LIKE ColumnA=&apos;*&amp;quot;34&amp;quot;*&apos;&lt;br&gt;
&lt;br&gt;
does one of those 2 work?</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.50261-762513</guid>
  	<pubDate>Mon, 06 Nov 2006 06:13:46 -0800</pubDate>
  	<dc:creator>derbs</dc:creator>
</item>
<item>
  	<title>By: SteveInMaine</title>
  	<link>http://ask.metafilter.com/50261/query-criteria-from-a-table#762543</link>	
  	<description>I don&apos;t think you can do this using a single query with the structure you&apos;re using.&lt;br&gt;
&lt;br&gt;
You could use an inner join if you turned the data on end. &lt;br&gt;
&lt;br&gt;
If your table had one column, named QueryID, four rows, and the rows looked like this...&lt;br&gt;
&lt;br&gt;
3&lt;br&gt;
5&lt;br&gt;
17&lt;br&gt;
34&lt;br&gt;
&lt;br&gt;
...you could join QueryID to the column in your other table that contains the values you wish to match.&lt;br&gt;
&lt;br&gt;
Is there a particular reason you need to reference one &amp;quot;cell&amp;quot;? If so, you&apos;ll likely need to write VBA, but it isn&apos;t difficult.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.50261-762543</guid>
  	<pubDate>Mon, 06 Nov 2006 06:48:35 -0800</pubDate>
  	<dc:creator>SteveInMaine</dc:creator>
</item>
<item>
  	<title>By: Zetetics</title>
  	<link>http://ask.metafilter.com/50261/query-criteria-from-a-table#762544</link>	
  	<description>Ah - sorry, no.&lt;br&gt;
&lt;br&gt;
To clarify, I&apos;m looking to get the values listed above from one table to be used as the criteria for a query to be applied to a different table.&lt;br&gt;
&lt;br&gt;
The table I&apos;m querying has a column that contains only one number, stored as a text value. So I want to get the list of values from tableA to to use as criteria for a query on tableB.  The query should return only the rows with 3, or 5, or 17, or 34 in one case,&lt;br&gt;
and only the rows with 1, or 12, or 15 in another case.  ( There are about 12 such cases)&lt;br&gt;
&lt;br&gt;
TableB:&lt;br&gt;
ID  Field1  Field 2&lt;br&gt;
1    text       3&lt;br&gt;
2    text       12&lt;br&gt;
3    text       34&lt;br&gt;
4    text       1&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
The criteria WHERE ColumnA IN( &amp;quot;3&amp;quot;, &amp;quot;5&amp;quot;,&amp;quot;17&amp;quot;, &amp;quot;34&amp;quot;)  works, I just want to be able to change the set of numbers searched for.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.50261-762544</guid>
  	<pubDate>Mon, 06 Nov 2006 06:51:02 -0800</pubDate>
  	<dc:creator>Zetetics</dc:creator>
</item>
<item>
  	<title>By: Zetetics</title>
  	<link>http://ask.metafilter.com/50261/query-criteria-from-a-table#762556</link>	
  	<description>SteveInMaine,&lt;br&gt;
&lt;br&gt;
I&apos;m considering using the approach you suggest but, basically, I&apos;m looking for a shortcut.  I&apos;m adding a step into the middle of a series of queries and calculations and I&apos;m looking make as few changes as possible to existing tables and queries.  I thought that there should be  quick way to do this, but if not, I&apos;ll do it with VBA.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.50261-762556</guid>
  	<pubDate>Mon, 06 Nov 2006 07:00:32 -0800</pubDate>
  	<dc:creator>Zetetics</dc:creator>
</item>
<item>
  	<title>By: jaded</title>
  	<link>http://ask.metafilter.com/50261/query-criteria-from-a-table#762610</link>	
  	<description>If access will do sub-selects then something like this should work:&lt;br&gt;
&lt;br&gt;
select some_column from TableB&lt;br&gt;
where some_other_column in (select columnA from TableA                                   where tableA.id=&apos;1&apos;)&lt;br&gt;
&lt;br&gt;
If you don&apos;t want the id=&apos;1&apos; condition, just remove it, or replace it as appropos.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.50261-762610</guid>
  	<pubDate>Mon, 06 Nov 2006 07:47:58 -0800</pubDate>
  	<dc:creator>jaded</dc:creator>
</item>
<item>
  	<title>By: Zetetics</title>
  	<link>http://ask.metafilter.com/50261/query-criteria-from-a-table#762712</link>	
  	<description>jaded,&lt;br&gt;
&lt;br&gt;
I think that approach should work as well  - as should a DLookUp in the same position.  However, when I run that query, it returns no rows.  &lt;br&gt;
 I suspect access does something odd to the value returned by the subquery before executing the main query.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.50261-762712</guid>
  	<pubDate>Mon, 06 Nov 2006 08:59:21 -0800</pubDate>
  	<dc:creator>Zetetics</dc:creator>
</item>
<item>
  	<title>By: treepour</title>
  	<link>http://ask.metafilter.com/50261/query-criteria-from-a-table#762930</link>	
  	<description>Off the top of my head, the only way I can think of to do this in a single query would be to write a convoluted formula that would analyze the string and return the nth item (and null if the nth item didn&apos;t exist).  You could then drop versions of this formulas into the criteria rows at the bottom of the query builder window to simulate the &amp;quot;in&amp;quot; function.  Probably would be easier to encapsulate this formula in a small VBA function, which could be called from the query.  E.g., if the function took 2 arguments, the text string of numbers and the lookup position, the criteria might look like:&lt;br&gt;
&lt;br&gt;
somefunction([ColumnA],1)&lt;br&gt;
somefunction([ColumnA],2)&lt;br&gt;
somefunction([ColumnA],3)&lt;br&gt;
etc . . .</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.50261-762930</guid>
  	<pubDate>Mon, 06 Nov 2006 12:16:13 -0800</pubDate>
  	<dc:creator>treepour</dc:creator>
</item>
<item>
  	<title>By: genghis</title>
  	<link>http://ask.metafilter.com/50261/query-criteria-from-a-table#763013</link>	
  	<description>Apologies for this, but my answer isn&apos;t going to be entirely helpful this time, so much as next time.&lt;br&gt;
&lt;br&gt;
This is exactly how you must &lt;em&gt;not&lt;/em&gt; store values in a database.  Whoever made this table is using Access but pretending it&apos;s Excel. That collection of strings belongs in a referenced table, not a column -- and all relational databases and SQL itself expect you to do so.&lt;br&gt;
&lt;br&gt;
To rephrase that so it sounds a little more helpful and a little less like chiding...  if this database is anything other than temporary you ought to &amp;quot;normalise&amp;quot; the data in it as quickly as possible.  Thankfully that can be scripted in about three minutes by anyone who can write basic ASP or any similar scripting language.&lt;br&gt;
&lt;br&gt;
Once it&apos;s done, your database can be made to stand up to bad data input in the future, and it becomes *much* simpler to write the query you&apos;re asking for.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.50261-763013</guid>
  	<pubDate>Mon, 06 Nov 2006 14:17:50 -0800</pubDate>
  	<dc:creator>genghis</dc:creator>
</item>
<item>
  	<title>By: Zetetics</title>
  	<link>http://ask.metafilter.com/50261/query-criteria-from-a-table#763113</link>	
  	<description>It looks like this is not as simple to do as I think it should be.&lt;br&gt;
&lt;br&gt;
I guess I&apos;ll just use VBA to build the SQL  then.&lt;br&gt;
&lt;br&gt;
Thanks all.&lt;br&gt;
&lt;br&gt;
And genghis, yes, I&apos;m trying to retrofit something into an already complicated database for people who would be much happier using excel - if only they could fit all their data into excel.&lt;br&gt;
I am well aware that I am not asking for the ideal way to handle this problem, but given the various constraints of the situation, this would have been, by far, the fastest solution.</description>
  	<guid isPermaLink="false">comment:ask.metafilter.com,2008:site.50261-763113</guid>
  	<pubDate>Mon, 06 Nov 2006 16:12:05 -0800</pubDate>
  	<dc:creator>Zetetics</dc:creator>
</item>

    </channel>
</rss>
