我可以在这个脚本上使用循环吗?我使用了相同的sql语句,但唯一的区别是字段的值(NAME)。
你可以帮助/帮助我
select
distinct
(select VALUE
from TABLE1
where O_ID = a.O_ID
and NAME ='Activity')Activity ,
(select VALUE
from TABLE1
where O_ID = a.O_ID
and NAME ='code') code ,
(select VALUE
from TABLE1
where O_ID = a.O_ID
and NAME ='object')object,
from TABLE1 a
where a.O_ID = 15850196
;
答案 0 :(得分:0)
我不认为在你的情况下你会通过使用with
子句获得足够的收益......但由于你无法创建函数,你可以按如下方式重新编写查询:
with
Activities as (select O_ID , VALUE from TABLE1 where NAME ='Activity'),
codes as (select O_ID , VALUE from TABLE1 where NAME ='code') ,
objects as (select O_ID , VALUE from TABLE1 where NAME ='object')
select
(select VALUE from Activities where a.O_ID = O_ID) as Activity,
(select VALUE from codes where a.O_ID = O_ID) as code,
(select VALUE from objects where a.O_ID = O_ID) as object
from TABLE1 a
where a.O_ID = 15850196
或
with
Activities as (select O_ID , VALUE from TABLE1 where NAME ='Activity'),
codes as (select O_ID , VALUE from TABLE1 where NAME ='code') ,
objects as (select O_ID , VALUE from TABLE1 where NAME ='object')
select
Activity.value, codes.value, objects.value
from TABLE1 a,Activities, codes, objects
where a.O_ID = 15850196 and
a.O_ID = Activities.O_ID(+) and
a.O_ID = codes.O_ID(+) and
a.O_ID = objects.O_ID(+)
答案 1 :(得分:0)
这似乎是那些烦人的键/值表之一。您需要ID 15850196的活动,代码和对象,但由于不存在包含这些列的表,因此您必须在键/值表中查找每个值。
一个常见的解决方案是条件聚合:
select
max(case when name ='Activity' then value end) as activity,
max(case when name ='code' then value end) as code,
max(case when name ='object' then value end) as object
from table1
where o_id = 15850196;