Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Where by one column

Author  Topic 

yoyosh
Starting Member

27 Posts

Posted - 2013-01-30 : 04:19:48
Why the following query is incorrect?

select (select top 1 date from pass_in_trip p where p.trip_no = t.trip_no) as date
from trip t
where date is not null

It says: Invalid column name 'date'.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-30 : 04:25:16
date is an alias created in same level so cant be used in where like that. so you need to tweak it like this

select *
from
(
select (select top 1 date from pass_in_trip p where p.trip_no = t.trip_no) as [date]
from trip t
)p
where [date] is not null


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

yoyosh
Starting Member

27 Posts

Posted - 2013-01-30 : 06:29:34
Thank you
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-30 : 06:48:28
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -