<p>I'm trying to perform a SQL query through a linked SSAS server.  The initial query works fine:</p>

<pre><code>SELECT "Ugly OLAP name" as "Value" 
FROM OpenQuery( OLAP, 'OLAP Query')
</code></pre>

<p>But if I try to add:</p>

<pre><code>WHERE "Value" &gt; 0
</code></pre>

<p>I get an error</p>

<blockquote>
  <p>Invalid column name 'Value'</p>
</blockquote>

<p>Any ideas what I might be doing wrong?</p>

<hr>

<p>So the problem was that the order in which elements of the query are processed are different that the order they are written.  According to this source:</p>

<p><a href="http://blogs.x2line.com/al/archive/2007/06/30/3187.aspx" rel="noreferrer">http://blogs.x2line.com/al/archive/2007/06/30/3187.aspx</a></p>

<p>The order of evaluation in MSSQL is:</p>

<ol>
<li>FROM</li>
<li>ON</li>
<li>JOIN</li>
<li>WHERE</li>
<li>GROUP BY</li>
<li>HAVING</li>
<li>SELECT</li>
<li>ORDER BY</li>
</ol>

<p>So the alias wasn't processed until after the WHERE and HAVING clauses.</p>

[{'answer_id': 46375, 'author': 'Till', 'author_id': 2859, 'author_profile': 'https://Stackoverflow.com/users/2859', 'pm_score': 0, 'selected': False, 'text': '<p>Oh, bummer. I just saw, you select AS FOO. Don\'t you need a HAVING claus in this case?</p>\n\n<pre><code>SELECT whatever AS value FROM table HAVING value &gt; 1;\n</code></pre>\n\n<p>I still would not use "value". But to be sure, look it up in your docs!</p>\n'}, {'answer_id': 46382, 'author': 'Chris Miller', 'author_id': 206, 'author_profile': 'https://Stackoverflow.com/users/206', 'pm_score': 5, 'selected': True, 'text': '<p>This should work:</p>\n\n<pre><code>SELECT A.Value\nFROM (\nSELECT "Ugly OLAP name" as "Value" \nFROM OpenQuery( OLAP, \'OLAP Query\')\n) AS a\nWHERE a.Value &gt; 0\n</code></pre>\n\n<p>It\'s not that Value is a reserved word, the problem is that it\'s a column alias, not the column name.  By making it an inline view, "Value" becomes the column name and can then be used in a where clause.</p>\n'}, {'answer_id': 46552, 'author': 'Andrew', 'author_id': 826, 'author_profile': 'https://Stackoverflow.com/users/826', 'pm_score': 3, 'selected': False, 'text': '<p>You\'re using "Value" as a column alias, and I don\'t think the alias can appear in the where clause. It\'s simply used to name the returned column value. Your where clause should refer to the original column name:</p>\n\n<pre><code>SELECT "Ugly OLAP name" as "Value" \nFROM OpenQuery( OLAP, \'OLAP Query\')\nWHERE "Ugly OLAP name" &gt; 0\n</code></pre>\n'}, {'answer_id': 10235088, 'author': 'John Dunagan', 'author_id': 28939, 'author_profile': 'https://Stackoverflow.com/users/28939', 'pm_score': 0, 'selected': False, 'text': '<p>I can vouch for leaving it out of GROUP BY. Good news is, it works just fine being a plain old selected alias.</p>\n'}]
['https://Stackoverflow.com/questions/46354', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1807/']