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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested select problem

Status
Not open for further replies.

taphonomist

Programmer
Jul 21, 2005
11
0
0
US
I get syntax errors with this

Code:
select but
from
(select
bte.buildfile, max(bt.buildtime) as but
from buildtimeestimator bte, buildtime bt
where bte.buildfile=bt.buildfile
group by bte.buildfile)

while this

Code:
select
bte.buildfile, max(bt.buildtime) as but
from buildtimeestimator bte, buildtime bt
where bte.buildfile=bt.buildfile
group by bte.buildfile

works fine. What's going on???

taphonomist
 
You are trying to select a table and this is not supported and will fail! You can only use select to select specific fields or by using the * all fields.

in your code 'but' is a derived table with 2 columns. You could use:

select *
from
(select
bte.buildfile, max(bt.buildtime) as but
from buildtimeestimator bte, buildtime bt
where bte.buildfile=bt.buildfile
group by bte.buildfile)
 
but is the second column in the two column table, the first being bte.buildfile (see the group by statement). In any case, I've also tried the code you've written with no luck.

taphonomist
 
whenever you have a derived table, you have to give it an alias

change your first query as follows --
Code:
select but
  from (
       select bte.buildfile
            , max(bt.buildtime) as but
         from buildtimeestimator bte
            , buildtime bt
        where bte.buildfile
            = bt.buildfile
       group 
           by bte.buildfile
       ) [b]as foo[/b]

r937.com | rudy.ca
 
Thanks much!! That'll do. It seems kind of odd that you have to give an alias to "transient" data... oh well, as long as it works!!

taphonomist
 
Ooops - sorry misread the brackets. How about putting a final 'AS Newtable' at the very end?

 
No worries, thanks to you both for the help!

taphonomist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top