ถ้าเรา order by string แบบธรรมดาๆ เราจะเจอปัญหาแบบนี้
การ order by string 10 จะมาก่อน 2 เพราะรหัส ASCII ของ 1 อยู่ก่อน 2
วิธีแก้
เปลี่ยนมา order by ด้วย sql function ที่ผมเขียนขึ้นมาใหม่ ได้เป็น
select data.*,
trim(replace(data.old_name, (numb), '')) new_name
from(
select name old_name,
regexp_substr(name, '^[0-9ก-ฮ\.]+\)*\s') numb
from MY_TABLE
) data
...
...
SELECT code
FROM ...
WHERE code IN('AAA', 'BBB', 'CCC')
...
...
ซึงเราสามารถตรวจสอบได้เพียง ครั้งละ 1 ค่าเท่านั้น (single value)SELECT trim(regexp_substr('String1,String2,String3', '[^,]+', 1, LEVEL)) str_2_tab
FROM dual
CONNECT BY LEVEL <= regexp_count('String1,String2,String3', ',')+1
thank you : http://stackoverflow.com/questions/14328621/oracle-10g-splitting-string-into-multiple-rows
select gp, LISTAGG(name, ', ') WITHIN GROUP (ORDER BY name) name from( select 'A' gp, 1 name from dual UNION select 'A' gp, 2 name from dual UNION select 'A' gp, 3 name from dual UNION select 'A' gp, 4 name from dual UNION select 'B' gp, 5 name from dual UNION select 'B' gp, 6 name from dual UNION select 'B' gp, 7 name from dual UNION select 'B' gp, 8 name from dual ) group by gpลอง run แบบนี้ดูครับ
select gp, RTRIM (XMLAGG (XMLELEMENT (e, name || ', ')).EXTRACT ('//text()'), ', ') name
from(
select 'A' gp, 1 name from dual
UNION
select 'A' gp, 2 name from dual
UNION
select 'A' gp, 3 name from dual
UNION
select 'A' gp, 4 name from dual
UNION
select 'B' gp, 5 name from dual
UNION
select 'B' gp, 6 name from dual
UNION
select 'B' gp, 7 name from dual
UNION
select 'B' gp, 8 name from dual
)
group by gp
ถ้าทำแล้วมันมี string ที่ซ้ำกันแล้วเราต้องการลบตัวที่ซ้ำ ใช้ ตัวนี้ช่วยครับ
...
... /* replace YOUR_STRING */
...
REGEXP_REPLACE(REGEXP_REPLACE(regexp_replace( YOUR_STRING ,'([^,]+)(, \1)+', '\1'),',{2,}',','),'^,|,$','')
...
...
Thank you : http://stackoverflow.com/questions/468990/how-can-i-combine-multiple-rows-into-a-comma-delimited-list-in-oracle
select trunc(months_between(sysdate,dob)/12) year,
trunc(mod(months_between(sysdate,dob),12)) month,
trunc(sysdate-add_months(dob,trunc(months_between(sysdate,dob)/12)*12+trunc(mod(months_between(sysdate,dob),12)))) day
from (
Select to_date('15122000','DDMMYYYY') dob
from dual
);
outputcreate or replace procedure delete_from_starts_with(p_schema varchar2, p_prefix varchar2) as
begin
for record in (select x.table_name origin_name
from dba_tables x
where x.owner = p_schema
and x.table_name like (p_prefix || '_%')
) loop
execute immediate ('delete from ' || record.origin_name);
end loop;
end;
example to use
begin
delete_from_starts_with('MY_SCHEMA', 'MY_TABLE_NAME_PREFIX');
end;
SELECT *
FROM
(
SELECT /*+ FIRST_ROWS */ data.*, ROWNUM rnum
FROM (
/* custom your query */
) data
WHERE ROWNUM <= 20
)
WHERE rnum > 10
create or replace function get_columns_name(tab_name VARCHAR2) return VARCHAR2 is
val VARCHAR2(3200);
begin
for col in (Select COLUMN_NAME from user_tab_columns where table_name = tab_name) loop
val := val || ',' || col.column_name;
end loop;
return substr(val, 2);
end;
exampleset serveroutput on
begin
dbms_output.put_line ('tb_user --> ' || get_columns_name('tb_user'));
-- tb_user --> id,name,username,password,version
end;
no
|
my_group
|
group_count
|
1
|
A
|
1
|
2
|
A
|
2
|
3
|
A
|
3
|
4
|
A
|
4
|
5
|
B
|
1
|
6
|
B
|
2
|
7
|
B
|
3
|
8
|
C
|
1
|
9
|
C
|
2
|
10
|
D
|
1
|
begin
for record in (select columnA, columnB, columnC from TEST_TABLE) loop
--
-- call store procedure in store package
-- and pass variable (columnA, columnB and columnC) into store procedure
--
store_package.store_procedure(record.columnA, record.columnB, record.columnC);
end loop;
end;
วันนี้ผมเจอกับเหตุการณ์นี้ครับ คือพอดีผมเขียนคำสั่ง sql ขึ้นมา
id
|
drg_code
|
department_id
|
1
|
0892
|
1
|
2
|
0694
|
1
|
3
|
0782
|
2
|
4
|
0321
|
1
|
5
|
0120
|
1
|
6
|
1001
|
1
|
7
|
0985
|
2
|
8
|
0325
|
2
|
9
|
0695
|
3
|
10
|
0127
|
4
|
11
|
0364
|
4
|
12
|
0697
|
5
|
13
|
0247
|
1
|
14
|
0324
|
1
|
15
|
0364
|
2
|
select id,
drg_code,
department_id,
row_number() over (partition by drg_code order by department_id, drg_code) drg_sequence
from drug_code
merge into TABLE_A
using (
select b.column1 columnB1,
b.column2 columnB2
from TABLE_B b
) on (TABLE_A.columnA1 = columnB1)
when matched
then update
set TABLE_A.columnA2 = columnB2;
-- -- -- select to_char(sysdate, 'YYYY', 'nls_calendar=''Thai Buddha'' nls_date_language = Thai') year from dual -- -- --
//
INSERT INTO DATABASE_NAME.table_A (field1, field2, field3, ...)
SELECT DATABASE_NAME.table_B.field1,
DATABASE_NAME.table_B.field2,
DATABASE_NAME.table_B.field3,
...
FROM DATABASE_NAME.table_B
WHERE ...
//
/* */ select 'drop table '||table_name||' cascade constraints;' from user_tables; /* */