Updated November, 23 2023
SQL#
Outer join operators#
Outer Join operators like (+)
are translated into OUTER JOIN
ANSI SQL Syntax. For example:
1--Oracle
2SELECT *
3 FROM table1 t1, table2 t2, table3 t3
4 WHERE t1.name(+) = t2.name
5 AND t1.name = t3.name;
ROWNUM/ROWID pseudo columns#
ROWID
is translated into OID
:
1--Oracle
2SELECT ROWID, last_name
3 FROM employees
4 WHERE department_id = 20;
ROWNUM
inSELECT
columns list is translated into a window functionrow_number
.ROWNUM
inWHERE
ofSELECT
is translated intoLIMIT
if possible.
For example:
1--Oracle
2SELECT item_num, order_num
3 FROM items
4 WHERE item_num < 10
5 AND ROWNUM < 6
6 AND order_num > 100;
ROWNUM
in UPDATE
is translated into a runtime function call calculating the next value.
1--Oracle
2UPDATE employees SET employee_id = ROWNUM, manager_id = ROWNUM;
Other usages aren’t supported.
CREATE SEQUENCE#
PostgreSQL has CREATE SEQUENCE
syntax with some differences:
1. The limit for MAX VALUE
in Postgres is 9223372036854775807.
2. Remove next clauses NOORDER
NOCACHE
GLOBAL
KEEP
NOKEEP
SESSION
SCALE
.
3. Replace: NOCYCLE
-> NO CYCLE
NOMINVALUE
-> NO MINVALUE
NOMAXVALUE
-> NO MAXVALUE
UNIQUE in SELECT statement#
UNIQUE
is the Oracle’s synonym for DISTINCT
. So we replace UNIQUE
with DISTINCT
.
PIVOT clause#
For emulation, we transform PIVOT into Decode form with aggregation and translate this:
1--Oracle
2SELECT * FROM
3(SELECT EXTRACT(YEAR FROM order_date) year, order_mode, order_total FROM orders)
4PIVOT
5(SUM(order_total) FOR order_mode IN ('direct' AS Store, 'online' AS Internet));
UNPIVOT clause#
Oracle unpivot single column
Example:
1--Oracle
2SELECT * FROM sale_stats
3 UNPIVOT INCLUDE NULLS(
4 quantity
5 FOR product_code
6 IN (
7 product_a AS 'A',
8 product_b AS 'B',
9 product_c AS 'C'
10 )
11 );
To achieve this result in PostgreSQL, we will define an array of values that will act as the values that represent each quarterly column, followed by an array that specifies each of the columns that correspond to those quarters. We will unnest both of those arrays, and that will give us paired values for each quarter and the amount of that quarter.
Note
If the original query uses an asterisk we need to read the metadata and exclude columns that are used in UNPIVOT clause
Ignore NULLS
For ignoring NULLS, we should wrap the translated query into the top query with the condition.
Oracle EXCLUDE NULLS.
1--Oracle
2SELECT * FROM sale_stats
3 UNPIVOT ( --EXCLUDE NULLS by default or we can explicit specify
4 quantity
5 FOR product_code
6 IN (
7 product_a AS 'A',
8 product_b AS 'B',
9 product_c AS 'C'
10 )
11 );
Oracle unpivot multiple columns.
1--Oracle
2SELECT * FROM sale_stats
3UNPIVOT (
4 (quantity, amount)
5 FOR product_code
6 IN (
7 (a_qty, a_value) AS 'A',
8 (b_qty, b_value) AS 'B'
9 )
10);