I am rather new with SQL
and I can't for the life of me figure out why I can't do what I'm trying here. I'm trying get the least number of free seats in a package-trip, and group them by the package ID.
The first of the subqueries work, but the second is the one I can't figure out. It says
Unkown column SuperPaket in 'where clause'.
select Paket.PaketID as "SuperPaket", Beskrivning, Resa.AvgångStad, Resa.AvgångTid, Resa.AvgångDatum,
(select AnkomstTid from Resa where Resa.ResID in
(select ResID from PaketResa where PaketResa.PaketID = SuperPaket and Ordningsnr =
(select max(Ordningsnr) from PaketResa where PaketResa.PaketID = SuperPaket))) "AnkomstTid",
(select min(LedigaPlatser) from
(select sum(AntalPlatser - Count) "LedigaPlatser", ResID from(
select Bokning.ResID, AntalPlatser, sum(Bokning.AntalBiljetter) as Count from
(Resa inner join Bokning on Resa.ResID = Bokning.ResID) where Bokning.ResID in
(select PaketResa.ResID from PaketResa where PaketResa.PaketID = SuperPaket)
group by Bokning.ResID order by Count desc)
as CountTable group by ResID)
as T) "LedigaPlatser"
from ((Paket inner join PaketResa on Paket.PaketID = PaketResa.PaketID) inner join Resa on PaketResa.ResID = Resa.ResID) group by Paket.PaketID;
Why does this work for the first subquery but not the second one?
Update.. The error seems to appear when I put a "sub-subquery" in a from clause. I don't know how to rewrite the problematic query to fix this problem though, and I would really appreciate some help. Thanks..