473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Table alias in nested query

Hello

I am trying to write an SQL statement that uses two derived tables / subqueries in a following way:

SELECT * FROM tbl A
...
INNER JOIN
(SELECT * FROM tbl2
WHERE col =
(SELECT MIN(col2) FROM tbl2
WHERE col3 = A.acol ...))

The problem is that such a statement is syntactically incorrect: the A.acol column is not accessible and I am getting following error:

" The multi-part identifier "A.acol" could not be bound."

So the questions I am trying to answer are:
(a) is it possible to use a table alias in second level sql subquery
(b) if not, how to solve this problem simply?

Thank you very much!

Tom
Jun 24 '07 #1
2 5233
debasisdas
8,127 Expert 4TB
Can you please post only your query part for my reference.
Jun 25 '07 #2
Can you please post only your query part for my reference.
Sure. I can post directly the query causing the problem.
Expand|Select|Wrap|Line Numbers
  1.            SELECT TE.*,
  2.             TE.HistoryId AS TreatmentEntryHistoryId,
  3.             TE.HistoryValidToByUserId AS TreatmentEntryValidToByUserId,
  4.             ET.*,
  5.             [FoFormInstance].*
  6.             FROM
  7.             [History_PeTreatmentEntry] TE
  8.  
  9.             INNER JOIN
  10.             (SELECT MAX(HistoryId) AS HistoryId FROM [History_PeTreatmentEntry] GROUP BY TreatmentEntryId) G
  11.             ON TE.HistoryId = G.HistoryId
  12.  
  13.  
  14.             INNER JOIN [History_PeTreatmentInstance]
  15.             ON [History_PeTreatmentInstance].TreatmentInstanceId = TE.TreatmentInstanceId
  16.  
  17.  
  18.             INNER JOIN
  19.             (SELECT TEA.* FROM [History_PeTreatmentEntry] TEA
  20.             WHERE HistoryId=
  21.             (SELECT MIN(HistoryId)
  22.             FROM [History_PeTreatmentEntry] Y
  23.  
  24.             -- Here is the problem! TE.TreatmentEntryId is syntactically incorrect
  25.             WHERE Y.TreatmentEntryId = TE.TreatmentEntryId GROUP BY TreatmentEntryId)) FirstTE
  26.  
  27.             ON (TE.TreatmentEntryId = FirstTE.TreatmentEntryId)
  28.             ....
  29.  
Jun 25 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mahesh Hardikar | last post by:
Hi , Oracle 8.1.7.0.0 on HP-UX 11.0 I am a newbie as far as PL-SQL is concerned . I have Sybase/MSSQL T-SQL background. We have a report which uses a select statement . This select...
3
by: Marcus | last post by:
Hi I have a very complex sql query and a explain plan. I found there is a full table scan in ID=9 9 8 TABLE ACCESS (FULL) OF 'F_LOTTXNHIST' (Cost=84573 Card=185892...
0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
14
by: John | last post by:
Hi all, I am doing the change from having worked in Oracle for a long time to MS SQL server and am frustrated with a couple of simple SQL stmt's. Or at least they have always been easy. The...
5
by: ahokdac-sql | last post by:
Hi, I'm adapting access queries to sql server and I have difficulties with the following pattern : query1 : SELECT * FROM Query2 WHERE A=@param1 query 2: SELECT * FROM Table2 WHERE B=@param2 ...
5
by: Arvin Portlock | last post by:
I can't come up with a query that works. Can anyone help? Conceptually the relationships are easy to describe. I have a table for books (Entries), a table for authors (Authors), and a linking...
3
by: Rodríguez Rodríguez, Pere | last post by:
Hello, I think I have found a query problem when the query has an alias for a table and use alias item and table name. I ilustrate the problem with a simple table and query. prr=# create...
1
by: Steve C | last post by:
Hi, I'm having problems constructing a nested join. It's quite complex, so here's a simplfied example of the problem. Any thoughts on what I'm doig wrong - or if I've got the whole approach...
3
by: Frank Swarbrick | last post by:
I was just messing around trying to learn things and attempted the following: select brch_nbr , sum(case when post_flag != 'P' then amount else 0 end) as sum_amount from film.film_transactions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.