sql 多列的大小写条件
sql Case condition for multiple columns
我创建了一个查询以获取下面的列作为输出
SELECT person_number,
second_year_ltp,
second_year_salary,
one_year_bonus,
one_year_salary,
one_year_ltp,
third_year_bonus,
three_year_ltp,
three_year_salary,
current_ltp,
current_salary,
current_bonus,
current_year,
previous_year,
second_year,
third_year,
fourth_year
FROM comp_sal
如果 sysdate >= '01-04-2022',我想更改多个列的值,我无法编写以下代码,因为它会报错 -
select person_number,
CASE
WHEN to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022'
THEN
current_bonus one_year_bonus,
current_ltip one_year_ltp,
current_salary one_year_salary,
one_year_bonus second_year_bonus,
one_year_ltp second_year_ltp,
one_year_salary second_year_salary,
second_year_bonus third_year_bonus,
second_year_ltp three_year_ltp,
second_year_salary three_year_salary
else
second_year_ltp,
second_year_salary,
one_year_bonus,
one_year_salary,
one_year_ltp,
third_year_bonus,
three_year_ltp,
three_year_salary,
current_ltip,
current_salary
end
from comp_sal
我是否必须为每一列编写案例?有更好的方法吗?
是的,您已经为每一列编写了单独的 case 表达式。或者,您可以编写两个单独的 SQL 语句,一个在 where
子句中包含您的条件,另一个在它的反面,并使用 union all
[=12 组合两个查询=]
您需要为每一列写一个案例陈述。
select person_number,
(CASE WHEN to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022' THEN current_ltip ELSE second_year_ltp END) one_year_bonus,
(CASE WHEN to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022' THEN current_salary ELSE second_year_salary END) one_year_ltp,
(CASE WHEN to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022' THEN one_year_salary ELSE one_year_bonus END) one_year_salary
from comp_sal
或者您可以编写两个查询,然后将两者与 union all 合并。两个查询中的列数应按适当的顺序匹配。
SELECT person_number,
current_bonus one_year_bonus,
current_ltip one_year_ltp,
current_salary one_year_salary,
one_year_bonus second_year_bonus,
one_year_ltp second_year_ltp,
one_year_salary second_year_salary,
second_year_bonus third_year_bonus,
second_year_ltp three_year_ltp,
second_year_salary three_year_salary
from comp_sal where to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022'
union all
select person_number,
second_year_ltp,
second_year_salary,
one_year_bonus,
one_year_salary,
one_year_ltp,
third_year_bonus,
three_year_ltp,
three_year_salary,
current_ltip
from comp_sal where to_char(sysdate,'dd-mm-yyyy') < '01-04-2022'
如果是oracle,作为替代,你可以这样写成pl/sql块,如下:
declare
v_query varchar2(4000);
begin
if to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022' then
-- select one set of columns
v_query := 'SELECT person_number,
current_bonus one_year_bonus,
current_ltip one_year_ltp,
current_salary one_year_salary,
one_year_bonus second_year_bonus,
one_year_ltp second_year_ltp,
one_year_salary second_year_salary,
second_year_bonus third_year_bonus,
second_year_ltp three_year_ltp,
second_year_salary three_year_salary ';
else
-- select another set of columns
v_query := 'SELECT person_number, second_year_ltp,
second_year_salary,
one_year_bonus,
one_year_salary,
one_year_ltp,
third_year_bonus,
three_year_ltp,
three_year_salary,
current_ltip,
current_salary ';
end if;
-- CONCAT FROM CLAUSE IN THE QUERY
v_auery := v_query ||' FROM comp_sal ';
-- run the query and store the result set in a table
execute immediate 'CREATE TABLE result_set as '||v_query;
-- your final result will be in the table result_set
end;
我创建了一个查询以获取下面的列作为输出
SELECT person_number,
second_year_ltp,
second_year_salary,
one_year_bonus,
one_year_salary,
one_year_ltp,
third_year_bonus,
three_year_ltp,
three_year_salary,
current_ltp,
current_salary,
current_bonus,
current_year,
previous_year,
second_year,
third_year,
fourth_year
FROM comp_sal
如果 sysdate >= '01-04-2022',我想更改多个列的值,我无法编写以下代码,因为它会报错 -
select person_number,
CASE
WHEN to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022'
THEN
current_bonus one_year_bonus,
current_ltip one_year_ltp,
current_salary one_year_salary,
one_year_bonus second_year_bonus,
one_year_ltp second_year_ltp,
one_year_salary second_year_salary,
second_year_bonus third_year_bonus,
second_year_ltp three_year_ltp,
second_year_salary three_year_salary
else
second_year_ltp,
second_year_salary,
one_year_bonus,
one_year_salary,
one_year_ltp,
third_year_bonus,
three_year_ltp,
three_year_salary,
current_ltip,
current_salary
end
from comp_sal
我是否必须为每一列编写案例?有更好的方法吗?
是的,您已经为每一列编写了单独的 case 表达式。或者,您可以编写两个单独的 SQL 语句,一个在 where
子句中包含您的条件,另一个在它的反面,并使用 union all
[=12 组合两个查询=]
您需要为每一列写一个案例陈述。
select person_number,
(CASE WHEN to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022' THEN current_ltip ELSE second_year_ltp END) one_year_bonus,
(CASE WHEN to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022' THEN current_salary ELSE second_year_salary END) one_year_ltp,
(CASE WHEN to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022' THEN one_year_salary ELSE one_year_bonus END) one_year_salary
from comp_sal
或者您可以编写两个查询,然后将两者与 union all 合并。两个查询中的列数应按适当的顺序匹配。
SELECT person_number,
current_bonus one_year_bonus,
current_ltip one_year_ltp,
current_salary one_year_salary,
one_year_bonus second_year_bonus,
one_year_ltp second_year_ltp,
one_year_salary second_year_salary,
second_year_bonus third_year_bonus,
second_year_ltp three_year_ltp,
second_year_salary three_year_salary
from comp_sal where to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022'
union all
select person_number,
second_year_ltp,
second_year_salary,
one_year_bonus,
one_year_salary,
one_year_ltp,
third_year_bonus,
three_year_ltp,
three_year_salary,
current_ltip
from comp_sal where to_char(sysdate,'dd-mm-yyyy') < '01-04-2022'
如果是oracle,作为替代,你可以这样写成pl/sql块,如下:
declare
v_query varchar2(4000);
begin
if to_char(sysdate,'dd-mm-yyyy') >= '01-04-2022' then
-- select one set of columns
v_query := 'SELECT person_number,
current_bonus one_year_bonus,
current_ltip one_year_ltp,
current_salary one_year_salary,
one_year_bonus second_year_bonus,
one_year_ltp second_year_ltp,
one_year_salary second_year_salary,
second_year_bonus third_year_bonus,
second_year_ltp three_year_ltp,
second_year_salary three_year_salary ';
else
-- select another set of columns
v_query := 'SELECT person_number, second_year_ltp,
second_year_salary,
one_year_bonus,
one_year_salary,
one_year_ltp,
third_year_bonus,
three_year_ltp,
three_year_salary,
current_ltip,
current_salary ';
end if;
-- CONCAT FROM CLAUSE IN THE QUERY
v_auery := v_query ||' FROM comp_sal ';
-- run the query and store the result set in a table
execute immediate 'CREATE TABLE result_set as '||v_query;
-- your final result will be in the table result_set
end;