1. "GROUP BY" clause is mandatory to use approx_* functions.
2. Approx_count & Approx_sum functions has to be used with "HAVING" clause containing approx_rank function.
3. Both approx_* functions and non approx_* aggregate functions are not allowed in same select statement.
4. "ORDER BY" clause within approx_rank function should use only approx_* functions only (+ with "DESC" keyword only).
5. "HAVING" clause should contain "<= number" or "< number" conditions only. Follow this format only ('APPROX_RANK(PARTITION BY ... ORDER BY ... DESC) <(=) n')
There are many situation, where the data from external sources(like text, csv, JSON, XML etc.,) needs to be loaded into database.This can be achieved by many ways. Though there are many ways, each option is suited for particular scenarios only. This is mainly due to the difference in the source data and target data structures.
For eg, we might need different mechanism to read text file vs XML files. The mechanism needs to be decided depending on the nature/type of source data.
Given below the possible ways to load data into oracle database.
Database ClientTools (like SQL developer, TOAD etc., support loading data from files into tables)
Program/Script ( Using any "Programming language" (Like Java etc.,) or "Scripting Language" (Like shell script, python etc.,))
SQL Loader (Oracle provided utility )
External Table ( Oracle provided option to read data from external files as if they present in table)
PLSQL Program ( Oracle supplied UTL_FILE package to do file handling operations like read, write, copy and remove operations on operating system files)
ETL Tools ( Lot of ETL tools are available to map, extract and load the data from external files into oracle database)
Of all the options given above, option 3,4 & 5 are oracle provided mechanism to load data into oracle database.
Here is a course on Udemy to teach about oracle supplied options to load the data from external source into oracle database.
This course covers in detail about SQL Loader, UTL_FILE & External table concepts with example.
Functions can be defined in the "WITH" clause and can be used in select statement from 12.1 onwards.
WITH
FUNCTION fn_cube (p_no NUMBER) RETURN NUMBER
AS
BEGIN
RETURN power(p_no,3);
END;
SELECT
100 AS num,
fn_cube(10)
FROM
dual;
/
Another Example of "WITH" clause function
WITH
FUNCTION avg_sal (p_deptno NUMBER) RETURN NUMBER AS
v_avg_sal NUMBER;
BEGIN
SELECT
AVG(sal)
INTO v_avg_sal
FROM
emp
WHERE
deptno = p_deptno;
RETURN v_avg_sal;
END avg_sal;
SELECT
deptno,
dname,
loc,
round(avg_sal(deptno) ) avg_sal -- Calling the function
FROM
dept;
/
No DMLs are allowed in function while using in select statements. However DMLs can be used with autonomous_transaction functions.
CREATE TABLE temp_1 (
col1 NUMBER
);
WITH
FUNCTION avg_sal (p_deptno NUMBER) RETURN NUMBER AS
PRAGMA autonomous_transaction;
v_avg_sal NUMBER;
BEGIN
SELECT
AVG(sal)
INTO v_avg_sal
FROM
emp
WHERE
deptno = p_deptno;
INSERT INTO temp_1 VALUES ( 1 );
COMMIT;
RETURN v_avg_sal;
END avg_sal;
SELECT
deptno,
dname,
loc,
round(avg_sal(deptno) ) avg_sal -- Calling the function
FROM
dept;
/
Example of using procedure and function in WITH clause, however procedures cannot be called directly in select statements. Procedure can be accessed via the calling functions as show below.
WITH
PROCEDURE pr_cube (p_no IN OUT NUMBER)
AS
BEGIN
p_no := power(p_no,3);
END;
FUNCTION fn_cube (p_no NUMBER) RETURN NUMBER AS
lv_num NUMBER := p_no;
BEGIN
pr_cube(lv_num);
RETURN lv_num;
END;
SELECT
100 AS num,
fn_cube(10)
FROM
dual;
/
WITH
PROCEDURE pr_cube (p_no IN OUT NUMBER) AS
PRAGMA autonomous_transaction;
BEGIN
p_no := power(p_no,3);
INSERT INTO temp_1 VALUES ( 1 );
COMMIT;
END;
FUNCTION fn_cube (p_no NUMBER) RETURN NUMBER AS
lv_num NUMBER := p_no;
BEGIN
pr_cube(lv_num);
RETURN lv_num;
END;
SELECT
100 AS num,
fn_cube(10)
FROM
dual;
/
select case when json_equal('{}','{ }') then 'SAME JSON' else 'DIFFERENT JSON' end JSON_COMP_RESULT
from dual;
select case when json_equal('{"A":null}','{"A":""}') then 'SAME JSON' else 'DIFFERENT JSON' end JSON_COMP_RESULT
from dual;
select case when json_equal('{"EMPNAME":"SCOTT"}','{"EMPNAME":"SCOTT"}') then 'SAME JSON' else 'DIFFERENT JSON' end JSON_COMP_RESULT
from dual;
select case when json_equal('{"EMPNAME":"SCOTT"}','{"EMPNAME":"Scott"}') then 'SAME JSON' else 'DIFFERENT JSON' end JSON_COMP_RESULT
from dual;
select case when json_equal('{"EMPID":"1000","EMPNAME":"KING"}','{"EMPID":"1000","EMPNAME":"KING"}') then 'SAME JSON' else 'DIFFERENT JSON' end JSON_COMP_RESULT
from dual;
select case when json_equal('{"EMPID":"1000","EMPNAME":"KING"}','{"EMPNAME":"KING","EMPID":"1000"}') then 'SAME JSON' else 'DIFFERENT JSON' end JSON_COMP_RESULT
from dual;
select case when json_equal('{"NUMLIST":[1,2,3,4,5,6]}','{"NUMLIST":[1,2,3,4,5,6]}') then 'SAME JSON' else 'DIFFERENT JSON' end JSON_COMP_RESULT
from dual;
select case when json_equal('{"NUMLIST":[1,2,3,4,5,6]}','{"NUMLIST":[6,5,4,3,2,1]}') then 'SAME JSON' else 'DIFFERENT JSON' end JSON_COMP_RESULT
from dual;
CREATE TABLE json_sample_data_table (
s_no NUMBER NOT NULL,
json_1 VARCHAR2(100),
json_2 VARCHAR2(100),
CONSTRAINT json_chk_1 CHECK ( json_1 IS JSON ),
CONSTRAINT json_chk_2 CHECK ( json_2 IS JSON )
);
insert into json_sample_data_table values(1,'{}','{ }');
insert into json_sample_data_table values(2,'{"A":null}','{"A":""}');
insert into json_sample_data_table values(3,'{"A":""}','{"A":""}');
insert into json_sample_data_table values(4,'{"EMPNAME":"SCOTT"}','{"EMPNAME":"SCOTT"}');
insert into json_sample_data_table values(5,'{"EMPID":"1000","EMPNAME":"KING"}','{"EMPID":"1000","EMPNAME":"KING"}');
insert into json_sample_data_table values(6,'{"EMPID":"1000","EMPNAME":"KING"}','{"EMPNAME":"KING","EMPID":"1000"}');
insert into json_sample_data_table values(7,'{"NUMLIST":[1,2,3,4,5,6]}','{"NUMLIST":[1,2,3,4,5,6]}');
insert into json_sample_data_table values(8,'{"NUMLIST":[1,2,3,4,5,6]}','{"NUMLIST":[6,5,4,3,2,1]}');
commit;
select * from json_sample_data_table
where json_1 = json_2;
select * from json_sample_data_table
where json_equal(json_1,json_2);
select * from json_sample_data_table
where not json_equal(json_1,json_2);
select s_no, json_1,json_2, case when json_equal(json_1,json_2) then 'SAME JSON' else 'DIFFERENT JSON' end JSON_COMP_RESULT
from json_sample_data_table;
I am Siva Jeevan.LK
[LEARN | CODE | TRAIN | SHARE]
Passionate to answer oracle SQL & PLSQL related questions.
In my day to day job, I work with technologies including Oracle, Java, Python, MongoDB, talend and UNIX. I am passionate about "DATA", “SQL” , coding & training.
In my spare time, I teach database technologies , ETL etc. I am very glad that you are reading my Profile, and I value your time as well as mine. Looking forward to see you in my videos at Siva Academy
Passionate to answer oracle SQL & PLSQL related questions.
You can contact me via My youtube channel (Siva Academy) or drop an email at siva.k.academy@gmail.com
Happy to Learn & Help :-)