我創建了一個查詢以獲取以下列作為輸出
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
我必須為每一列寫案例嗎?有更好的方法嗎?
uj5u.com熱心網友回復:
您需要為每列撰寫一個案例陳述句。
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'
uj5u.com熱心網友回復:
是的,您已經為每一列撰寫了單獨的 case 運算式。或者,您可以撰寫兩條單獨的 SQL 陳述句,一條在where子句中包含您的條件,另一條與它相反,然后使用union all
uj5u.com熱心網友回復:
如果是 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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/434217.html
標籤:sql 甲骨文 案子 oracle-sqldeveloper
下一篇:從子查詢SQL列印資料
