Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Server View

Status
Not open for further replies.

ashab02

Programmer
Jun 28, 2005
87
GB
Hello

I am currently writing a view in sql server using the front end view building tool and I am trying to within the criteria use the 'IN' Statement which then does a sub query as follows 'IN (SELECT TOP 1 DATE FROM CUSTOMER MA WHERE DATE >'12/05/2006' AND CUSTOMERCODE = MA.CUSTOMER CODE). It is basically needs to join on its self but it keeps bring an error of :
'The column prefix 'MA' does not match with a table or alias name used in the query'

Can anyone help?
 
Two points, firstly you dont need to use IN when your inner statement is only returning 1 row.
Second, ensure you alias all tables in outer query and reference in inner query.

e/g
Code:
SELECT * FROM MyOuterTable tb1
IN (SELECT TOP 1 DATE FROM CUSTOMER MA WHERE MA.DATE >'12/05/2006' AND TB1.CUSTOMERCODE = MA.CUSTOMER CODE)




"I'm living so far beyond my income that we may almost be said to be living apart
 
One correction to hmckillop's code......the WHERE is missing:

Code:
SELECT * FROM MyOuterTable tb1
WHERE DATE IN (SELECT TOP 1 DATE FROM CUSTOMER MA WHERE MA.DATE >'12/05/2006' AND TB1.CUSTOMERCODE = MA.CUSTOMER CODE)

-SQLBill

Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top