如何在 WHERE 子句中实现 CASE SELECT
How to implement CASE SELECT in WHERE clause
我有两个 table T1
和 T2
场景 1:
Table T1 的 C1 列的值为 terminal1、terminal2、terminal3
Select 来自 T1 的 C1
1号航站楼
2号航站楼
3号航站楼
Table T2 的 C2 列的值为 terminal1、terminal2
Select 来自 T2 的 C2
1号航站楼
2号航站楼
塞纳里奥 2
Table T2 有时可以为空
Select 来自 T2 的 C2
(空)
(空)
我的输出必须是
当 Senario 1 然后
1号航站楼
2号航站楼
当 Senario 2 然后
1号航站楼
2号航站楼
3号航站楼
即当 table T1 和 T2 具有共同值时,我只需要匹配值
和
当 Table T2 有空值时,我想要所有的值
来自 table T1
您的问题需要适当的格式,并详细说明您面临的问题。我假设,您需要一个语句,它显示哪些值相交,如果它们不相交 - 仅显示表 A。对于该子句,您需要使用内部联接和案例结构。例如:
select
ter."HEATHROW_TERMINAL",
case
when lok."HEATHROW_TERMINAL" is not null then
'Terminal is in both tables'
else
'Terminalis only in main table'
end terminal_state
from
test_terminal_v ter,
test_lookup_v lok
where
ter."HEATHROW_TERMINAL" = lok."HEATHROW_TERMINAL"(+)
其中,test_terminal_v (A) 的值为“5”、“4”、“2”、“3”,test_lookup_v (B) 的值为“4”、“2”、 “3”。
希望对你有帮助。
评论后:有几种方法。 1)可以使用临时表。创建一个,代码为:
begin
insert into test_term_tmp
select
ter.terminal
from
test_terminal_v ter,
test_lookup_v lok
where
ter.terminal = lok.terminal;
if sql%rowcount = 0 then
insert into TEST_TERM_TMP
select
ter.terminal
from
test_terminal_v ter;
end if;
end;
或 2) 使用类型,例如:
declare
type t_term_tab is table of number index by binary_integer;
l_term t_term_tab;
l_i number := 1;
begin
for l_rec1 in (
select
ter.terminal
from
test_terminal_v ter,
test_lookup_v lok
where
ter.terminal = lok.terminal)
loop
l_term(l_i) := l_rec1.terminal;
l_i := l_i + 1;
end loop;
if l_term.first is null then
l_i := 1;
for l_rec2 in (
select
ter.terminal
from
test_terminal_v ter)
loop
l_term(l_i) := l_rec2.terminal;
l_i := l_i + 1;
end loop;
end if;
end;
根据您的数据测试性能。希望对你有帮助。
看来你需要的是左外连接。外连接允许我们有条件地连接表。
此版本的查询将为 SP_RETAIL_TRANSACTION
中的所有行 return STORE_TERMINAL_LOCATION
并且仅匹配 SP_TEMP_LOOKUP.AIRPORT_TERMINAL
.
的值
SELECT DISTINCT A.STORE_TERMINAL_LOCATION, B.AIRPORT_TERMINAL
FROM SP_RETAIL_TRANSACTION A,
left outer join SP_TEMP_LOOKUP B
on A.STORE_TERMINAL_LOCATION = B.AIRPORT_TERMINAL
谢谢大家,我想我找到了答案
select 不同的 T1.C1、T2.C2
从 T1 ,T2
其中 T2.C2=T1.C1 或 T2.C2 为空;
它在我的代码中的什么地方
select 不同 A.STORE_TERMINAL_LOCATION,B.AIRPORT_TERMINAL
从SP_RETAIL_TRANSACTIONA,SP_TEMP_LOOKUPB
其中 B.AIRPORT_TERMINAL=A.STORE_TERMINAL_LOCATION 或 B.AIRPORT_TERMINAL 为空;
我有两个 table T1
和 T2
场景 1:
Table T1 的 C1 列的值为 terminal1、terminal2、terminal3
Select 来自 T1 的 C1
1号航站楼
2号航站楼
3号航站楼
Table T2 的 C2 列的值为 terminal1、terminal2
Select 来自 T2 的 C2
1号航站楼
2号航站楼
塞纳里奥 2
Table T2 有时可以为空
Select 来自 T2 的 C2
(空)
(空)
我的输出必须是
当 Senario 1 然后
1号航站楼
2号航站楼
当 Senario 2 然后
1号航站楼
2号航站楼
3号航站楼
即当 table T1 和 T2 具有共同值时,我只需要匹配值
和
当 Table T2 有空值时,我想要所有的值 来自 table T1
您的问题需要适当的格式,并详细说明您面临的问题。我假设,您需要一个语句,它显示哪些值相交,如果它们不相交 - 仅显示表 A。对于该子句,您需要使用内部联接和案例结构。例如:
select
ter."HEATHROW_TERMINAL",
case
when lok."HEATHROW_TERMINAL" is not null then
'Terminal is in both tables'
else
'Terminalis only in main table'
end terminal_state
from
test_terminal_v ter,
test_lookup_v lok
where
ter."HEATHROW_TERMINAL" = lok."HEATHROW_TERMINAL"(+)
其中,test_terminal_v (A) 的值为“5”、“4”、“2”、“3”,test_lookup_v (B) 的值为“4”、“2”、 “3”。 希望对你有帮助。
评论后:有几种方法。 1)可以使用临时表。创建一个,代码为:
begin
insert into test_term_tmp
select
ter.terminal
from
test_terminal_v ter,
test_lookup_v lok
where
ter.terminal = lok.terminal;
if sql%rowcount = 0 then
insert into TEST_TERM_TMP
select
ter.terminal
from
test_terminal_v ter;
end if;
end;
或 2) 使用类型,例如:
declare
type t_term_tab is table of number index by binary_integer;
l_term t_term_tab;
l_i number := 1;
begin
for l_rec1 in (
select
ter.terminal
from
test_terminal_v ter,
test_lookup_v lok
where
ter.terminal = lok.terminal)
loop
l_term(l_i) := l_rec1.terminal;
l_i := l_i + 1;
end loop;
if l_term.first is null then
l_i := 1;
for l_rec2 in (
select
ter.terminal
from
test_terminal_v ter)
loop
l_term(l_i) := l_rec2.terminal;
l_i := l_i + 1;
end loop;
end if;
end;
根据您的数据测试性能。希望对你有帮助。
看来你需要的是左外连接。外连接允许我们有条件地连接表。
此版本的查询将为 SP_RETAIL_TRANSACTION
中的所有行 return STORE_TERMINAL_LOCATION
并且仅匹配 SP_TEMP_LOOKUP.AIRPORT_TERMINAL
.
SELECT DISTINCT A.STORE_TERMINAL_LOCATION, B.AIRPORT_TERMINAL
FROM SP_RETAIL_TRANSACTION A,
left outer join SP_TEMP_LOOKUP B
on A.STORE_TERMINAL_LOCATION = B.AIRPORT_TERMINAL
谢谢大家,我想我找到了答案
select 不同的 T1.C1、T2.C2
从 T1 ,T2
其中 T2.C2=T1.C1 或 T2.C2 为空;
它在我的代码中的什么地方
select 不同 A.STORE_TERMINAL_LOCATION,B.AIRPORT_TERMINAL
从SP_RETAIL_TRANSACTIONA,SP_TEMP_LOOKUPB
其中 B.AIRPORT_TERMINAL=A.STORE_TERMINAL_LOCATION 或 B.AIRPORT_TERMINAL 为空;