SQLTeam.com | Weblogs | Forums

Subqueries

I have the following code consisting of two queries from the same table. I want to combine the results with a sub query. The first query is :

declare @cnt int = 1

while @cnt < 13
begin

select maand,min(Stasie1) as minStasie1, max(stasie1) as maksStasie1, avg(stasie1) as gemStasie1
from GekombineerAlleMaande
where maand = @cnt

group by maand

set @cnt = @cnt +1

end;

The second is :

select min(datum) from
GekombineerAlleMaande
where stasie1 is not null

Both these queries works, but I want to combine the results.
My sub query is in brackets after the where statement of the first query :

declare @cnt int = 1

while @cnt < 13
begin

select maand,min(Stasie1) as minStasie1, max(stasie1) as maksStasie1, avg(stasie1) as gemStasie1
from GekombineerAlleMaande
where maand = @cnt
(select min(datum) from
GekombineerAlleMaande
where stasie1 is not null)
group by maand

set @cnt = @cnt +1

end;

When execute the above I get the error : Incorrect syntax near the keyword 'group'.
What am I missing?

Regards

select query1.*, query2.*
from (
    select maand,min(Stasie1) as minStasie1, max(stasie1) as maksStasie1, avg(stasie1) as gemStasie1
    from GekombineerAlleMaande
    where maand between 1 and 12
) as query1
cross join (
    select min(datum) 
    from GekombineerAlleMaande
    where stasie1 is not null
) as query2
1 Like

Thank you Scott. I just needed to insert a group by and column name for query 2 and it worked. Thank you you very much.
Here is the working code for someone else's benefit.

select query1., query2.
from (
select maand,min(Stasie1) as minStasie1, max(stasie1) as maksStasie1, avg(stasie1) as gemStasie1
from GekombineerAlleMaande
where maand between 1 and 12
group by maand
) as query1
cross join (
select min(datum) as begindatum
from GekombineerAlleMaande
where stasie1 is not null
) as query2