21

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

SELECT "Ugly OLAP name" as "Value" 
FROM OpenQuery( OLAP, 'OLAP Query')

But if I try to add:

WHERE "Value" > 0

I get an error

Invalid column name 'Value'

Any ideas what I might be doing wrong?


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:

http://blogs.x2line.com/al/archive/2007/06/30/3187.aspx

The order of evaluation in MSSQL is:

  1. FROM
  2. ON
  3. JOIN
  4. WHERE
  5. GROUP BY
  6. HAVING
  7. SELECT
  8. ORDER BY

So the alias wasn't processed until after the WHERE and HAVING clauses.

ConcernedOfTunbridgeWells
  • 64,444
  • 15
  • 143
  • 197
dmo
  • 3,993
  • 6
  • 35
  • 39

4 Answers4

17

This should work:

SELECT A.Value
FROM (
SELECT "Ugly OLAP name" as "Value" 
FROM OpenQuery( OLAP, 'OLAP Query')
) AS a
WHERE a.Value > 0

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.

Chris Miller
  • 4,809
  • 4
  • 33
  • 50
6

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:

SELECT "Ugly OLAP name" as "Value" 
FROM OpenQuery( OLAP, 'OLAP Query')
WHERE "Ugly OLAP name" > 0
Andrew
  • 11,894
  • 12
  • 69
  • 85
0

I can vouch for leaving it out of GROUP BY. Good news is, it works just fine being a plain old selected alias.

John Dunagan
  • 1,445
  • 3
  • 18
  • 30
0

Oh, bummer. I just saw, you select AS FOO. Don't you need a HAVING claus in this case?

SELECT whatever AS value FROM table HAVING value > 1;

I still would not use "value". But to be sure, look it up in your docs!

Till
  • 22,236
  • 4
  • 59
  • 89