다음 표가 있습니다.
SELECT name, location_id, store_date, type, fact_count FROM table_test ORDER BY name, store_date;
이름 | location_id | store_date | 유형 | fact_count 파리 | 466 | 2015-12-01 | 0 | 255 파리 | 466 | 2015-12-01 | 1 | 256 베를린 | 329 | 2015-12-01 | 1 | 248 베를린 | 329 | 2015-12-01 | 0 | 244 프라하 | 201 | 2015-12-01 | 1 | 107 프라하 | 201 | 2015-12-01 | 0 | 102
동일한 행에 유형 + 값을 나열하려면 어떻게해야합니까 (항상 두 가지 유형 만 있습니다)?
이름 | location_id | store_date | type_0 | fact_count_for_type_0 | type_1 | fact_count_for_type_1 파리 | 466 | 2015-12-01 | 0 | 255 | 1 | 256 베를린 | 329 | 2015-12-01 | 0 | 244 | 1 | 248 프라하 | 201 | 2015-12-01 | 0 | 102 | 1 | 107
SELECT
name,
location_id,
store_date,
sum(fact_count * (type = 0)::int) as fact_count_type_0,
sum(fact_count * (type = 1)::int) as fact_count_type_1
FROM table_test
group by 1,2,3
ORDER BY name, store_date;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다