2017-03-18 6 views
0

이 내 테이블 구조는, 한 행의 데이터를 얻기 위해 키와 값을 사용하여 동일한 테이블에 참여

내가

id journal_id  property  prop_key  old_value      value 
405 252    attr   color   1         0 
372 252    attr  updated_at  2017-03-18 03:43:34 UTC  2017-03-18 03:43:34 UTC 
402 252    attr  sprint_id  5         0 
prop_key = '색상'등의 키 값과 같은 테이블을 결합하는 wabt입니다

결과를이 양식으로 원합니다.

id   color  sprint_id   start_date 
372   2   5    2017-03-18 03:43:34 UTC 

나는 다음과 같은

select t.id, 
     max(case when a.prop_key='color' then a.value end) attr_val1, 
     max(case when a.prop_key='sprint_id' then a.value end) attr_val2, 
     max(case when a.prop_key='updated_at' then a.value end) attr_val3 
from journal_details t 
left join journal_details a on t.id = a.id 
where t.journal_id=242 
group by t.id 

Select 
    journal_details.id, 
    a1.value as color, 
    a2.value as sprint_id, 
    a3.value as start_date 
from journal_details 
    left join (select * from journal_details where prop_key='color') a1 on journal_details.id=a1.id 
    left join (select * from journal_details where prop_key='sprint_id') a2 on journal_details.id=a2.id 
    left join (select * from journal_details where prop_key='start_date') a3 on journal_details.id=a3.id 
    where journal_details.journal_id=242 

하려고하지만 난 만족스러운 결과를 얻을 수 didnot.

답변

1

자기 조인이 필요하지 않습니다.

이 시도 :

select min(id), 
    max(case when prop_key = 'color' then value end) attr_val1, 
    max(case when prop_key = 'sprint_id' then value end) attr_val2, 
    max(case when prop_key = 'updated_at' then value end) attr_val3 
from journal_details 
where journal_id = 242 
+0

그러나이 http://prntscr.com/eldkp5 같은 다른 값으로 5 행을 제공하지만 난 모든 결과 –

+0

@Navjot로 한 행을 원하는 - journal_id 열에 그룹화 하시겠습니까? 아마 (id) 아마도? 결과의 id 열의 값은 무엇입니까? 귀하의 질문에, 당신은 372의 예상 출력을 보여 주지만, 그것은 어디에서 왔는가? 입력 한 데이터가 없습니다. – GurV

+0

그 ID입니다. 그것이있을 수 있습니다 4022. 질문 업데이트 –