İfade olarak kullanılan alt sorgudan birden fazla satır nasıl döndürülür

0

Soru

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

Buna sahibim, bedroom_count tek bir satır değil, bu yüzden bu hatayı veriyor

HATA: ifade olarak kullanılan bir alt sorgu tarafından döndürülen birden çok satır

Bu durumda bu sonucu almam gerekiyor bunun için başka bir çözüm var mı?

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

Hata, verilen property_id (550) için birinci veya ikinci alt sorgunun 1'den fazla satır döndürmesinden kaynaklanır. Yorumlarınızdan

Sonuç olarak hepsini istiyorum

Sanırım ihtiyacın olan şey her iki masaya da katılmak. Bunu dene

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

Görünüşe göre gerçekten masalara katılmak istiyorsun. Yatak odasında bir tablo veya diğer sayıları istediğiniz gibi olsa da, dış tabloları birleştirmek gerekir.

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;

Veya kullanın 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;

(Property_type null olabilir, bu sorguların bununla başa çıkmak için bazı ayarlamaları gerekir.)

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

bu, birisinin ihtiyacı olması durumunda sorunumun cevabı

2021-11-24 07:43:36

Tamam, bu gerçekten aradığınız bir toplama. Bir dahaki sefere bir soru sorduğunuzda, lütfen örnek verileri ve beklenen sonucu gösterin, böylece ne sorduğunuzu anlarız.
Thorsten Kettner

evet üzgünüm, bu benim ilk seferim )))) çok teşekkürler
Grigor Martiros

Zaten cevabından çok şey öğrendim.
Grigor Martiros

Diğer dillerde

Bu sayfa diğer dillerde

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