首先利用下面的 SQL Command,即可將刪除 Table 之 SQL command 全部產生出來:
select 'drop table '||table_name||' cascade constraints;' from user_tables order by table_name
接下來批次執行所有產生出來的 SQL command 即可
2015年1月21日 星期三
2014年8月18日 星期一
SQL: 找出某個欄位重複出現的資料
select
[Column_Name],
count(*)
from
[Table_Name]
group by
[Column_Name] having count(*) > 1
其中[Column_Name]若是該 Table 中應為 Unique 之欄位,且該欄位未設定為 unique 時,可利用此方法找出重複出現的資料
[Column_Name],
count(*)
from
[Table_Name]
group by
[Column_Name] having count(*) > 1
其中[Column_Name]若是該 Table 中應為 Unique 之欄位,且該欄位未設定為 unique 時,可利用此方法找出重複出現的資料
2013年5月6日 星期一
Oracle, SQL 以多個欄位為 Key, 取出 table 中另一欄位為 max 之資料
table_1 資料表如下圖:
如上資料表中,此資料表中並沒有設定 Primary Key欄位,但可由 column_1, column_2, column_3, column_4, column_5, column_6 的組合當成 Key 值,但仍有可能有重覆之資料,如表中所示,column_4 = 8 , 9 , 10 的資料都有多筆,但取資料時又希望依據相同的數據,將這些重覆的過濾掉只留下 check_column 數字最大的那筆時,可利用下面的SQL完成。
select *
from table_1
where (column_1, column_2, column_3, column_4, column_5, column_6, check_column)
in (
select
column_1,
column_2,
column_3,
column_4,
column_5,
column_6,
max(check_column)
from table_1
group by
column_1,
column_2,
column_3,
column_4,
column_5,
column_6
)
結果如下圖:
2012年9月13日 星期四
[Oracle SQL] 串接某欄位的查詢結果
● 以特定符號串接某欄位的查詢結果 ( 這邊以逗號 "," 為串接符號 )
● 將查詢出來的多筆資料結合成一筆
以圖表示即
→

原查詢之 Oracle SQL 語法如下:
select
Column_Name
from
Table_Name
where
column1 = 'xxx';
串接語法如下:
with mv as (
select
Column_Name
from
Table_Name
where
column1 = 'xxx'
)
select
ltrim(extract(xmlagg(xmlelement("Column_Name",','||Column_Name)),'/Column_Name/text()').getstringval(),',') Column_Name
from mv;
● 將查詢出來的多筆資料結合成一筆
以圖表示即
→

原查詢之 Oracle SQL 語法如下:
select
Column_Name
from
Table_Name
where
column1 = 'xxx';
串接語法如下:
with mv as (
select
Column_Name
from
Table_Name
where
column1 = 'xxx'
)
select
ltrim(extract(xmlagg(xmlelement("Column_Name",','||Column_Name)),'/Column_Name/text()').getstringval(),',') Column_Name
from mv;
2012年4月24日 星期二
Oracle SQL 數子左邊位數不足補 0
函式解說:
LPAD(X,Y[,Z])
把X的左邊加入字元Z(預設的字元是空格),令它的長度成為Y。
範例:把1左邊不足5位數之部份補上0
select LPAD('1',5,'0') from dual;
結果為 00001
LPAD(X,Y[,Z])
把X的左邊加入字元Z(預設的字元是空格),令它的長度成為Y。
範例:把1左邊不足5位數之部份補上0
select LPAD('1',5,'0') from dual;
結果為 00001
訂閱:
文章 (Atom)

