를 반환하는 방법보다 더 많은 중 하나에서 행 하위 사용으로 표현

0

질문

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

나는 이 bedroom_count 이 아닌 하나의 행,그래서 그것이 오류를 제공합니다

오류가:하나 이상의 행 반환에 의해 하위 표현으로 사용됩

필요한 결과를 얻는 경우 다른 솔루션을 위한 이?

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

에 오류가 온다는 사실에서 첫 번째 중 하나 또는 두 번째 하위 이상 1 에 대한 행정 property_id(550). 에서 당신의 댓글

내가 원하는 모든 이들의 결과로

나는 당신이 필요한 것은 왼쪽 가입하 모두와 테이블이 있습니다. 이

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

그것은 소리처럼 당신이 정말로 원하는 가입하는 테이블이 있습니다. 당신이 원하는대로의 침실 계산 한 테이블에서 또는 다른 하지만,당신은 외부에 가입 테이블이 있습니다.

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;

거나 사용 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;

(If property_type null 이 될 수 있습니다,이러한 쿼리를 필요로 할 것이 일부 조정하여 처리합니다.)

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

이것은 답을 내 문제의 경우에 누군가가 그것을 필요로

2021-11-24 07:43:36

좋아하는,그래서 그것은 참으로 집계를 찾고 있었던 것입니다. 다음 시간 당신이 질문을 보여주십시오 샘플 데이터 및 예상 결과,그래서 우리는 이해 당신이 무엇을 요청하고 있습니다.
Thorsten Kettner

네 죄,이것은 내 처음))))덕분에 많은 나
Grigor Martiros

많이 배웠에서 당신의 답변은 어쨌든
Grigor Martiros

다른 언어로

이 페이지는 다른 언어로되어 있습니다

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