选择独立的行并将其显示为一行(ORACLE SQL)
我有一个名为requesttool.request_detail的表,用于存储由列request_ID中的值标识的实体的属性。表requesttool.request_detail有一个名为ATTRIBUTE_ID的列,该列指示另一个名为ATTRIBUTE_value的列的相应行中存储的内容。例如,如果给定行的属性_ID='259',则名称将存储在属性_值的相应行中 以下是requesttool.request\u细节在实践中的表现: 我要做的是为3个不同的属性_ID和给定的请求_ID(比如4500161635)提取存储在属性_value中的值,并将它们显示在一行中,如下所示: 我尝试了以下代码:选择独立的行并将其显示为一行(ORACLE SQL),sql,oracle,Sql,Oracle,我有一个名为requesttool.request_detail的表,用于存储由列request_ID中的值标识的实体的属性。表requesttool.request_detail有一个名为ATTRIBUTE_ID的列,该列指示另一个名为ATTRIBUTE_value的列的相应行中存储的内容。例如,如果给定行的属性_ID='259',则名称将存储在属性_值的相应行中 以下是requesttool.request\u细节在实践中的表现: 我要做的是为3个不同的属性_ID和给定的请求_ID(比如4
select
request_id,
case when attribute_id = '289' then attribute_value end as name,
case when attribute_id = '259' then attribute_value end as country,
case when attribute_id = '351' then attribute_value end as priority
from (
select a.request_id, a.attribute_id, a.attribute_value
from requesttool.request_detail a
where a.request_id='4500161635');
但由此我得到了一个空值的表,而不是一行:
你走在正确的轨道上。只有您需要聚合行,才能为每个请求获得一个结果行。\u id:
select
request_id,
max(case when attribute_id = '289' then attribute_value end) as name,
max(case when attribute_id = '259' then attribute_value end) as country,
max(case when attribute_id = '351' then attribute_value end) as priority
from requesttool.request_detail
where request_id = '4500161635'
group by request_id;
给定一个请求索引\u id+属性\u id,您可以通过在where子句中添加一个条件来加速此过程:
and attribute_id in ('289', '259', '351')
顺便问一下:请求id和属性id真的是字符串吗?或者为什么在数字上使用引号?试试这个
select
request_id,
MIN(case when attribute_id = '289' then attribute_value end) as name,
MIN(case when attribute_id = '259' then attribute_value end) as country,
MIN(case when attribute_id = '351' then attribute_value end) as priority
from (
select a.request_id, a.attribute_id, a.attribute_value
from requesttool.request_detail a
where a.request_id='4500161635')
GROUP BY request_id
您可以按请求id分组,并为所有3列选择最大值(大小写..)。