dbTalk Databases Forums  

select alias -- invalid column name

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss select alias -- invalid column name in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
sweetpotatop@yahoo.com
 
Posts: n/a

Default select alias -- invalid column name - 06-26-2007 , 10:02 AM






Hi,

I got 'Invalid Column Name NewCol1' when I query the following:

Select col1, col2, (some calculation from the fields) as NewCol1,
(some calculation from the fields) as NewCol2,
NewCol1 = NewCol2 from
Table1 inner join Table2 inner join Table3....
Where
.....

Basically, I want to find out if NewCol1 = NewCol2 after the
calculation

Any advice?

Thanks in advance. Your help would be greatly appreciated.
Wanda


Reply With Quote
  #2  
Old   
Ed Murphy
 
Posts: n/a

Default Re: select alias -- invalid column name - 06-26-2007 , 11:16 AM






sweetpotatop (AT) yahoo (DOT) com wrote:

Quote:
I got 'Invalid Column Name NewCol1' when I query the following:

Select col1, col2, (some calculation from the fields) as NewCol1,
(some calculation from the fields) as NewCol2,
NewCol1 = NewCol2 from
Table1 inner join Table2 inner join Table3....
Where
.....

Basically, I want to find out if NewCol1 = NewCol2 after the
calculation
You can try this:

select col1, col2, (...) as NewCol1, (...) as NewCol2,
case when NewCol1 = NewCol2 then 'equal' else 'not equal' end

but I don't think that works. This should definitely work:

select col1, col2, (...) as NewCol1, (...) as NewCol2,
case when (...) = (...) then 'equal' else 'not equal' end


Reply With Quote
  #3  
Old   
sweetpotatop@yahoo.com
 
Posts: n/a

Default Re: select alias -- invalid column name - 06-26-2007 , 11:27 AM



On Jun 26, 12:16 pm, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote:
Quote:
sweetpota... (AT) yahoo (DOT) com wrote:
I got 'Invalid Column Name NewCol1' when I query the following:

Select col1, col2, (some calculation from the fields) as NewCol1,
(some calculation from the fields) as NewCol2,
NewCol1 = NewCol2 from
Table1 inner join Table2 inner join Table3....
Where
.....

Basically, I want to find out if NewCol1 = NewCol2 after the
calculation

You can try this:

select col1, col2, (...) as NewCol1, (...) as NewCol2,
case when NewCol1 = NewCol2 then 'equal' else 'not equal' end

but I don't think that works. This should definitely work:

select col1, col2, (...) as NewCol1, (...) as NewCol2,
case when (...) = (...) then 'equal' else 'not equal' end
I just want to avoid the calculation again when it is already there as
my query takes a while to run already.



Reply With Quote
  #4  
Old   
Ed Murphy
 
Posts: n/a

Default Re: select alias -- invalid column name - 06-26-2007 , 12:41 PM



sweetpotatop (AT) yahoo (DOT) com wrote:

Quote:
On Jun 26, 12:16 pm, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote:
sweetpota... (AT) yahoo (DOT) com wrote:
I got 'Invalid Column Name NewCol1' when I query the following:
Select col1, col2, (some calculation from the fields) as NewCol1,
(some calculation from the fields) as NewCol2,
NewCol1 = NewCol2 from
Table1 inner join Table2 inner join Table3....
Where
.....
Basically, I want to find out if NewCol1 = NewCol2 after the
calculation
You can try this:

select col1, col2, (...) as NewCol1, (...) as NewCol2,
case when NewCol1 = NewCol2 then 'equal' else 'not equal' end

but I don't think that works. This should definitely work:

select col1, col2, (...) as NewCol1, (...) as NewCol2,
case when (...) = (...) then 'equal' else 'not equal' end

I just want to avoid the calculation again when it is already there as
my query takes a while to run already.
I think the server will recognize and optimize the duplication. If
you're concerned it won't, though, then you could use a temp table:

create table #foo (
col1 type, col2 type, NewCol1 type, NewCol2 type, match int
)

insert into #foo (col1, col2, NewCol1, NewCol2, match)
select col1, col2, (...), (...), 0

update #foo set match = 1 where NewCol1 = NewCol2


Reply With Quote
  #5  
Old   
Plamen Ratchev
 
Posts: n/a

Default Re: select alias -- invalid column name - 06-26-2007 , 02:51 PM



You can use a derived table, like this:

SELECT X.col1,
X.col2,
X.newcol1,
X.newcol2,
CASE WHEN X.newcol1 = X.newcol2
THEN 'Equal'
ELSE 'Not equal'
END AS compare
FROM (
SELECT col1,
col2,
<calculation1> AS newcol1,
<calculation2> AS newcol2
FROM Table1) AS X
INNER JOIN Table2
.....


HTH,

Plamen Ratchev
http://www.SQLStudio.com




Reply With Quote
  #6  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: select alias -- invalid column name - 06-26-2007 , 03:29 PM



On Tue, 26 Jun 2007 09:27:03 -0700, sweetpotatop (AT) yahoo (DOT) com wrote:

Quote:
On Jun 26, 12:16 pm, Ed Murphy <emurph... (AT) socal (DOT) rr.com> wrote:
sweetpota... (AT) yahoo (DOT) com wrote:
I got 'Invalid Column Name NewCol1' when I query the following:

Select col1, col2, (some calculation from the fields) as NewCol1,
(some calculation from the fields) as NewCol2,
NewCol1 = NewCol2 from
Table1 inner join Table2 inner join Table3....
Where
.....

Basically, I want to find out if NewCol1 = NewCol2 after the
calculation

You can try this:

select col1, col2, (...) as NewCol1, (...) as NewCol2,
case when NewCol1 = NewCol2 then 'equal' else 'not equal' end

but I don't think that works. This should definitely work:

select col1, col2, (...) as NewCol1, (...) as NewCol2,
case when (...) = (...) then 'equal' else 'not equal' end

I just want to avoid the calculation again when it is already there as
my query takes a while to run already.
Hi sweetpotatop,

Instead of using a temp table as Ed suggests, you can better use a
derived table:

SELECT col1, col2, NewCol1, NewCol2,
CASE WHEN NewCol1 = NewCol2 THEN 'equal' ELSE 'not equal' END
FROM (SELECT col1, col2, (...) AS NewCol1, (...) AS NewCol2
FROM YourTable
WHERE Something = SomethingElse) AS D;

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.