Come restituire più di una riga dalla subquery utilizzato come espressione

0

Domanda

select case 
         when p.property_type ='APARTMENT_COMMUNITY' 
           then (select fp.bedroom_count 
                 from floor_plans fp 
                 where fp.removed = false 
                 and fp.property_id=p.id) 
         else (select pu.bedroom_count 
               from property_units pu 
               where pu.removed = false 
               and pu.property_id=p.id) 
        end 
from properties p 
where p.id =550

Io ho questo,bedroom_count non è una singola riga,quindi dà questo errore

ERRORE: più di una riga restituita da una subquery utilizzata come espressione

Ho bisogno di ottenere questo risultato in quel caso c'è qualche altra soluzione per questo?

postgresql sql
2021-11-24 06:24:39
3
0

L'errore deriva dal fatto che sia primo o secondo subquery restituisce più di 1 riga per property_id (550). Dai vostri commenti

Io voglio che tutti loro come conseguenza

Sto cercando di indovinare che cosa avete bisogno è una left join con entrambe le tabelle. Prova questo

select p.property_type, coalesce(fp.bedroom_count, pu.bedroom_count) as bedroom_count
  from properties p
  left join floor_plans fp 
    on p.property_type = 'APARTMENT_COMMUNITY' and fp.removed = false and fp.property_id = p.id
  left join property_units pu
    on p.property_type <> 'APARTMENT_COMMUNITY' and pu.removed = false and pu.property_id = p.id
 where p.id = 550
2021-11-24 06:50:23
0

Suona come si vuole veramente per il join delle tabelle. Come si desidera che la camera conta da una tabella o l'altro, però, si dovrebbe outer join le tabelle.

select p.*, coalesce(fp.bedroom_count, pu.bedroom_count) as bedroom_count
from properties p
left join floor_plans fp on p.property_type = 'APARTMENT_COMMUNITY' 
                         and fp.property_id = p.id
                         and fp.removed = false 
left join property_units pu on p.property_type <> 'APARTMENT_COMMUNITY' 
                            and pu.property_id = p.id
                            and pu.removed = false 
where p.id = 550
order by p.id;

O utilizzare UNION ALL:

select p.*, fp.bedroom_count
from properties p
join floor_plans fp on fp.property_id = p.id and fp.removed = false 
where p.id = 550
and p.property_type = 'APARTMENT_COMMUNITY'
union all
select p.*, pu.bedroom_count
from properties p
join property_units pu on pu.property_id = p.id and pu.removed = false 
where p.id = 550
and p.property_type <> 'APARTMENT_COMMUNITY'
order by p.id;

(Se property_type può essere null, queste query avrà bisogno di qualche aggiustamento a che fare con questo.)

2021-11-24 06:51:04
0
select  case 
            when p.property_type ='APARTMENT_COMMUNITY' 
                then (  
                    select  array_agg(distinct fp.bedroom_count) 
                    from    floor_plans fp 
                    where   fp.removed = false 
                    and     fp.property_id=p.id ) 
            else (
                    select  (array_agg(distinct pu.bedroom_count)) 
                    from    property_units pu 
                    where   pu.removed = false 
                    and pu.property_id=p.id ) 
        end 
from    properties p 
where   p.id =550

questa è la risposta al mio problema nel caso in cui qualcuno ne ha bisogno

2021-11-24 07:43:36

Va bene, così è, infatti, un processo di aggregazione che si sta cercando. La prossima volta si faccia una domanda, si prega di mostrare i dati di esempio e risultati attesi, in modo da capire che cosa si sta chiedendo.
Thorsten Kettner

sì scusa ,questa è la mia prima volta )))) grazie mille mi sarà
Grigor Martiros

Ho imparato molto dalla tua risposta comunque
Grigor Martiros

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................