我正在尝试实现以下结果输出
I'm trying to achieve below result output
我们能否通过使用 PIVOT 或其他方式获得以下所需的 table。我正在尝试将 table 以下转换为所需的输出,如下所示。
数据集:
question_id element_id
1 john
1 bran
1 o_siera
2 brook
2 joseph
2 o_daniel
2 o_cody
3 derick
3 james
3 sophia
3 o_sandra
3 o_ashley
期望的结果:
question_id element_id element
1 john o_siera
1 bran o_siera
2 brook o_daniel
2 joseph o_daniel
3 derick o_sandra
3 james o_sandra
3 sophia o_sandra
或
或者我们可以这样实现吗
question_id element_id element
1 john o_siera
1 bran
2 brook o_daniel,o_cody
2 joseph
3 derick o_sandra, o_ashley
3 james
3 sophia
我建议:
select t.*, max_o
from (select t.*,
max(case when element_id like 'o\_%' then element_id end) over (partition by question_id) as max_o
from t
) t
where element_id not like 'o\_%';
这不是一个理想的数据模型。除非以 'o_%' 开头的值是 element_id 而不是元素。
这没有经过测试。
select t1.question_id
,case when t1.element_id not like 'o_%' then t1.element_id else '' end element_id
,case when t2.element_id like 'o_%' then t2.element_id else '' end element
from table t1
join table t2 on t1.question_id=t2.question_id
我们能否通过使用 PIVOT 或其他方式获得以下所需的 table。我正在尝试将 table 以下转换为所需的输出,如下所示。
数据集:
question_id element_id
1 john
1 bran
1 o_siera
2 brook
2 joseph
2 o_daniel
2 o_cody
3 derick
3 james
3 sophia
3 o_sandra
3 o_ashley
期望的结果:
question_id element_id element
1 john o_siera
1 bran o_siera
2 brook o_daniel
2 joseph o_daniel
3 derick o_sandra
3 james o_sandra
3 sophia o_sandra
或
或者我们可以这样实现吗
question_id element_id element
1 john o_siera
1 bran
2 brook o_daniel,o_cody
2 joseph
3 derick o_sandra, o_ashley
3 james
3 sophia
我建议:
select t.*, max_o
from (select t.*,
max(case when element_id like 'o\_%' then element_id end) over (partition by question_id) as max_o
from t
) t
where element_id not like 'o\_%';
这不是一个理想的数据模型。除非以 'o_%' 开头的值是 element_id 而不是元素。
这没有经过测试。
select t1.question_id
,case when t1.element_id not like 'o_%' then t1.element_id else '' end element_id
,case when t2.element_id like 'o_%' then t2.element_id else '' end element
from table t1
join table t2 on t1.question_id=t2.question_id