简体   繁体   中英

Merging results in a row depending on another field

Below is the source table and the output I am trying to achieve.

在此处输入图片说明

I would like each another_table.id to only appear once in the output, with subsequent values displaying in new columns

This will give you the output, given the table in your question. With CTEs instead of subqueries for added clarity...

with intials as(
SELECT id, 
another_table.id as another_table_id,
value as field_1
FROM
main_table
WHEN
variable.id = 1),

finals as(
SELECT id, 
another_table.id as another_table_id,
value as field_1
FROM
main_table
WHEN
variable.id = 2)

SELECT
initials.id,
initials.another_table_id
initials.value field_1,
finals.value field_2
FROM
initials
LEFT OUTER JOIN
finals on initials.another_table_id = finals.another_table_id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM