oracle 重新命名列

2021-09-26 09:39:39 字數 1379 閱讀 5646

今天使用oracle資料庫重新命名列後發現新的列表無法在where條件下使用:

select count(1) from (select to_number(extract(hour from checktime)) 

as check_hour

from sams_checkinout where check_hour >= to_number(22))

查詢了一番發現oracle的確是不支援這樣操作的,可以把查詢出來的資料重新命名為一張表,再查詢表內的資料

with e as (select to_number(extract(hour from checktime)) 

as check_hour from sams_checkinout)

select count(1) from e where check_hour >= to_number(22);

with as的語法:

--針對乙個別名

with tmp as (select * from tb_name)

--針對多個別名

with

tmp as (select * from tb_name),

tmp2 as (select * from tb_name2),

t*** as (select * from tb_name3),

將重複使用的sql語句放到一張臨時表中,效率較高。

特別對於union all比較有用。(union 操作符用於合併兩個或多個 select 語句的結果集,union all 命令會列出所有的值)

因為union all的每個部分可能相同,但是如果每個部分都去執行一遍的話,則成本太高,所以可以使用with as短語,則只要執行一遍即可。

with

sql1 as (select to_char(a) s_name from test_tempa),

sql2 as (select to_char(b) s_name from test_tempb where not exists (select s_name from sql1 where rownum=1))

select * from sql1

union all

select * from sql2

union all

select 'no records' from dual

where not exists (select s_name from sql1 where rownum=1)

and not exists (select s_name from sql2 where rownum=1);

oracle表重新命名

使用方法 alter table test rename to test1 使用場景 對於乙個很大的日誌表,沒有做分割槽表,之前也從未清理過,現在需要清理,要求只保留3個月資料。如果直接用delete,oracle會記錄大量的日誌,並且有可能撐爆回滾表空間。最好的處理方法是新建乙個表,把想要的資料放...

python重新命名列名 Pandas對列進行重新命名

在處理dataframe資料的時候,經常需要對列進行重新命名,我們現在看一下到底如何進行重新命名操作。首先生成乙個dataframe。import pandas as pd province pd.dataframe province idcode省份 01a廣東 12b廣西 23c福建 34d福建...

檔案重新命名或者專案重新命名

在專案開發中 有時候某個專案的名稱中途需要更改一下 這個時候乙個乙個的去找對應的檔名然後更換在專案小的時候還可以 如果專案比較龐大的話那就相當複雜了 好在xcode有個重構的方法 方便操作 比如 把專案中status更名為 cdsinacellmodel 這個方法能夠把整個專案的status更名為c...