ERROR:SELECT pkg.id as id, pkg.name_en as name, pkg.descrshort_en as descrshort, pkg.descr_en as descr, pkg.travnote_en as travnote, validity1_out as v1, validity2_out as v2,validity3_out as v3,
t_country.name_en as countryname, currency, flg_nodepart,
case when (flg_nodepart<>'1') then 'ok' else '' end as show_dep,
pkg.url_photosmall, pkg.url_photo1, pkg.url_photo2, pkg.url_photo3, pkg.url_photo4,
minnights as minnights, nights as maxnights, case when (price_adult>0) then price_adult else 0 end as price,
case when (date_to<'1980-12-31' or date_to is null) then '1980-12-31' else date_to end as date_to,
case when (date_from<'1980-12-31' or date_from is null) then '1980-12-31' else date_from end as date_from,
case when (id_from<>3) then 'ok' else '' end as mostra_prezzo,
pd.descr_en as dep_desc,pd.info_en as dep_info,pd.include_en as dep_inc,pd.notinclude_en as dep_notinc, flg_new,
m_descr as meal FROM pkg
left join (
select id_package, max(date_to) as date_to
from pkg_details
where flg_active='1'
group by id_package
) as pd0 on pkg.id = pd0.id_package
left join (
select id_package, min(date_from) as date_from
from pkg_details
where ((date_from > now() or date_from<'1990-01-01 00:00:00')and date_from is not null) and flg_active='1'
group by id_package
) as pd1 on pkg.id = pd1.id_package
left join(
select id_package, max(nights) as nights
from pkg_details
group by id_package
) as pd2 on pkg.id = pd2.id_package
left join(
select id_package, min(nights) as minnights
from pkg_details
group by id_package
) as pd3 on pkg.id = pd3.id_package
left join(
select id_package, min(price_adult) as price_adult
from pkg_details
where flg_active='1'
group by id_package
) as pd4 on pkg.id = pd4.id_package
left join (
select m2.descr_en m_descr, id_package, id_meal, m1.descr_en, info_en, include_en, notinclude_en, flg_active
from
(
select DISTINCT ON (id_package) id_package, id_meal, descr_en, info_en, include_en, notinclude_en, flg_active
from pkg_details
) m1 left join pkg_meals m2 on id_meal= m2.id
) as pd on pkg.id = pd.id_package
left join t_country on pkg.id_country = t_country.id
WHERE true AND pkg.id=2471 and (pkg.flg_active = '1' or id_from='3') and ( date_from > now() or flg_nodepart = '1') and flg_italian<>'1'
and (validity1_out >= current_date or validity2_out >= current_date or validity3_out >= current_date) and pkg.name_en<>'' and pkg.name_en is not null
order by random()