select
t1.property_reg_num,
t1.property_owner_name_id
from property_table t1
inner join
(
select
property_reg_num,
property_owner_name_id,
MIN(property_record_on_year) min_year
from property_table
group by property_reg_num,
property_owner_name_id
) t2 on t2.property_reg_num = t1.property_reg_num
and t2.property_owner_name_id = t1.property_owner_name_id
and t1.property_record_on_year in (t2.min_year, t2.min_year+1, t2.min_year+2)
group by t1.property_reg_num,
t1.property_owner_name_id
select
t3.id,
t3.Customer,
t3.weight,
t4.others
from
(
select
t1.id,
t1.Customer,
t2.weight
from Order t1
inner join
(
select
Order_id,
sum(Weight) as weight
from Order_position
group by Order_id
) t2 on t2.Order_id = t1.id
) t3
inner join
(
select
Order_id,
sum(Price * Value) as others
from Order_other
group by Order_id
) t4 on t4.Order_id = t3.id
select
t3.Continent,
t3.Region,
sum(t3.Countries),
sum(t3.LifeDuration),
sum(t3.Population),
sum(t3.Cities),
sum(t4.Languages)
from
(
select
t1.Code,
t1.Continent,
t1.Region,
t1.Countries,
t1.LifeDuration,
t1.Population,
t2.Cities
from
(
select
Code,
Continent,
Region,
count(Name) as Countries,
ROUND(AVG(LifeExpectancy), 2) as LifeDuration,
SUM(Population) as Population
from country
group by Code,Continent,Region
) t1 left join
(
select
CountryCode,
count(Name) as Cities
from city
group by CountryCode
) t2 on t2.CountryCode = t1.Code
) t3 left join
(
select
CountryCode,
count(Language) as Languages
from countrylanguage
) t4 on t4.CountryCode = t3.Code
group by t3.Continent,t3.Region