Oracle:如何使用多个子查询优化SQL
TABLENAME:table 栏目:Oracle:如何使用多个子查询优化SQL,sql,oracle,subquery,query-optimization,query-performance,Sql,Oracle,Subquery,Query Optimization,Query Performance,TABLENAME:table 栏目: Id | name | c1 | c2 | c3 | c4 | b1 | b2 | b3 | b4 | time 查询: Select (case when select count(*) from s1 where s1.b1 = 1 then select top 1 from s1 where s1.b1 = 1 order by time desc else s2
Id | name | c1 | c2 | c3 | c4 | b1 | b2 | b3 | b4 | time
查询:
Select (case when select count(*) from s1 where s1.b1 = 1
then select top 1 from s1 where s1.b1 = 1 order by time desc
else s2.id
end) as COLUMNONE,
(case when select count(*) from s1 where s1.b2 = 1
then select top 1 from s1 where s1.b2 = 1 order by time desc
else s2.name
end) as COLUMNTWO,
(case when select count(*) from s1 where s1.b3 = 1
then select top 1 from s1 where s1.b3 = 1 order by time desc
else s2.c1
end) as COLUMNTHREE,
(case when select count(*) from s1 where s1.b4 = 1
then select top 1 from s1 where s1.b4 = 1 order by time desc
else s2.c2
end) as COLUMNFOUR
from
table s1, table s2
where s1.id = s2.id;
s1 table records
uniqueId | Id | name | c1 | c2 | c3 | c4 | b1 | b2 | b3 | b4 | time
a 12 abc qw tr cd hi 1 1 1 1 1
b 13 abc qw tr cd hi 1 1 1 1 2
c 14 abc qw tr cd hi 1 1 1 1 3
d 12 null null null cd hi 1 0 0 0 4
e 13 abcef qw tr cd hi 1 1 0 0 5
f 14 null null null cd hi 1 0 0 0 6
g 12 abcji qwer trft cd hi 1 1 1 0 7
h 13 null null null cd hi 1 0 0 0 8
i 14 abc qw tryr cdoi hi 1 0 1 1 9
j 12 abc qw trqw cdpl hi 1 0 0 1 10
k 13 abcij qw tr cd hi 1 1 0 0 11
l 14 abc qw troi cd hi 1 0 1 0 12
s2 table records will contain the latest records
Id | name | c1 | c2 | c3 | c4 | b1 | b2 | b3 | b4 | time
12 abc qw tr cdpl hi 1 0 0 1 10
13 abcij qw tr cd hi 1 1 0 0 11
14 abc qw troi cd hi 1 0 1 0 12
OUTPUT :
Id(COLUMNONE) | name(COLUMNTWO) | c1(COLUMNTHREE) | c2(COLUMNFOUR) |
12 abcji qw trqw
13 abcij qw tr
14 abc qw tryr
如果我有多个select子查询,有人能帮助我如何优化吗?表达此问题的更好方法可能是显示示例输入数据和您期望的输出。在我看来,此代码非常无效。真的是甲骨文吗?哪个版本?顺便说一句,
select
s在case
中没有与任何东西进行比较,缺少括号,这怎么可能是select
(虽然它看起来更像更新
)?-在ANSI-92 SQL标准中(超过25年前),旧样式的逗号分隔表列表样式已被正确的ANSIJOIN
语法所取代,并且其使用也已过时discouraged@Littlefoot我刚刚发布了一些它的伪代码。希望我编辑的问题能帮助你给我一些建议solution@Eyeself . . . 您的“查询”不会在任何数据库中运行。因此,似乎很难进行优化。语法错误返回得非常快。你想知道怎么做。我建议您提出一个新问题,提供示例数据、期望的结果,并解释要实现的逻辑。