Oracle connect by level complication - sql

I'm having difficulties with a query that's purpose is to split a row into two.
My sample data:
ID OLD NEW
---------- ------------------------ ------------------------
12 L-D / T-E L-E / T-E
13 L-D / T-E L-D / T-D
14 L-D / T-E L-E / T-D
Where the new and old values differ, I'd like to split them so my output should be:
id | OLD | NEW
12 | L-D | L-E //in this instance T hasn't changed so I will ignore it.
13 | T-E | T-D //similar to the above but this time only add T
14 | L-D | L-E //both values have changed, two rows required
14 | T-E | T-D
I've put together a 'working' example but the results are duplicated. I think the issue is with the connect by level, I'm quite new to Oracle so debugging is a touch difficult.
Any help is much appreciated.
This is the query I'm using to drop, create and run the query:
It runs fine in sqldeveloper but unsure if sqlplus will be impressed with it.
drop table t;
CREATE TABLE T
(id int,
old varchar2(24),
new varchar2(24))
;
INSERT ALL
INTO T (id, old, new)
VALUES (12, 'L-D / T-E', 'L-E / T-E')
INTO T (id, old, new)
VALUES (13, 'L-D / T-E', 'L-D / T-D')
INTO T (id, old, new)
VALUES (14, 'L-D / T-E', 'L-E / T-D')
SELECT * FROM dual
;
SELECT * FROM T;
--insert into t (id, old, new) values (1,'dasdsad', 'asdasd');
BEGIN
INSERT INTO t (id,old, new)
WITH DATA AS
(SELECT id,OLD, new
FROM t
WHERE
--multiple changes
(
(SUBSTR(OLD,3,1) <> SUBSTR(NEW, 3,1)
AND SUBSTR(OLD, 9) <> SUBSTR(NEW, 9)
)
OR
--row 2 old/new L are same, old/new T are not
(SUBSTR(OLD,3,1) = SUBSTR(NEW, 3,1)
AND SUBSTR(OLD, 9) <> SUBSTR(NEW, 9)
)
OR
--row 1
(SUBSTR(OLD,3,1) <> SUBSTR(NEW, 3,1)
AND SUBSTR(OLD, 9) = SUBSTR(NEW, 9)
)
)
)
SELECT id, trim(regexp_substr(OLD, '[^/]+', 1, LEVEL)) OLD,
trim(regexp_substr(NEW, '[^/]+', 1, LEVEL)) NEW
FROM DATA
CONNECT BY (LEVEL <= regexp_count(new, '/')+1);
END;
/

I personally wouldn't use a recursive syntax.
First I would create virtual columns (or a view) to eliminate cumbersome substring functions from the query, in this way:
ALTER TABLE t ADD ( OLD_LEFT AS ( Substr( OLD, 1, 3 )));
ALTER TABLE t ADD ( OLD_RIGHT AS ( Substr( OLD, 7, 3 )));
ALTER TABLE t ADD ( NEW_LEFT AS ( Substr( NEW, 1, 3 )));
ALTER TABLE t ADD ( NEW_RIGHT AS ( Substr( NEW, 7, 3 )));
or:
CREATE VIEW tt AS
SELECT ID, OLD, NEW,
Substr( OLD, 1, 3 ) As OLD_LEFT,
Substr( OLD, 7, 3 ) As OLD_RIGHT,
Substr( NEW, 1, 3 ) As NEW_LEFT,
Substr( NEW, 7, 3 ) As NEW_RIGHT
FROM t;
After the above simplification, the query is very straightforward, just:
SELECT id, OLD_LEFT , NEW_LEFT
FROM T
WHERE OLD_LEFT <> NEW_LEFT
UNION ALL
SELECT id, OLD_RIGHT , NEW_RIGHT
FROM T
WHERE OLD_RIGHT <> NEW_RIGHT
ORDER BY ID
Demo: http://sqlfiddle.com/#!4/2fdc4/2

Related

Multiple Sequence in SQL Oracle / Varchar2 ID with letters and digits

I am trying to 'generate' a Varchar ID that contains letters and digits too with a sequence, but I think there will be more sequences than one.
What I am looking forward to is something like this 'DJ_Digit_Digit_Letter_Letter_Letter'. An example would be DJ00WVX/DJ01HYZ/DJ99ZZZ. This is also my primary key so it would be good if I could not encounter any primary key errors.
I thought about working in ascii so I will generate numbers between 65 and 90 (A-Z) and Insert them into the column. Same for the numbers in ascii 45 to 57 or something like that. I don't know how I could use more sequences on the same column like 'letter_seq.nextvalue,digit_seq.nextvalue', i know that this example is wrong, but it was easier for me to explain it. Maybe all my thinking is wrong
In conclusion I need to get something that starts with DJ followed by 2 digits and 3 letters, all wrapped up in a primary key. It's ok to get like 20-30 unique entries, I think.
Thank you for your help!
You can create an INVISIBLE IDENTITY column and then create your letter-digit-letter sequence as a function generated from the invisible column:
CREATE TABLE table_name (
id_seq_value INT
INVISIBLE
GENERATED ALWAYS AS IDENTITY( START WITH 0 INCREMENT BY 1 MINVALUE 0 MAXVALUE 175799 )
NOT NULL
CONSTRAINT table_name__id_seq_value__u UNIQUE,
id VARCHAR2(7)
GENERATED ALWAYS AS (
CAST(
'DJ'
|| TO_CHAR( FLOOR( id_seq_value / POWER( 26, 3 ) ), 'FM00' )
|| CHR( 65 + MOD( FLOOR( id_seq_value / POWER(26, 2) ), 26 ) )
|| CHR( 65 + MOD( FLOOR( id_seq_value / POWER(26, 1) ), 26 ) )
|| CHR( 65 + MOD( id_seq_value, 26 ) )
AS VARCHAR2(7)
)
)
CONSTRAINT table_name__id__pk PRIMARY KEY,
name VARCHAR2(50)
);
Then you can do:
INSERT INTO table_name ( name ) VALUES ( 'Item1' );
INSERT INTO table_name ( name ) VALUES ( 'Item2' );
Then:
SELECT * FROM table_name;
Outputs:
ID | NAME
:------ | :----
DJ00AAA | Item1
DJ00AAB | Item2
(If you want to increment the digits and the letters together then change the IDENTITY to INCREMENT BY 17577 (263+1).)
db<>fiddle here
To me, it looks like this:
SQL> create table test (id varchar2(10));
Table created.
SQL> create sequence seq1;
Sequence created.
SQL> insert into test (id)
2 with
3 a as (select chr(64 + level) la from dual
4 connect by level <= 26),
5 b as (select chr(64 + level) lb from dual
6 connect by level <= 26),
7 c as (select chr(64 + level) lc from dual
8 connect by level <= 26)
9 select 'DJ' || lpad(seq1.nextval, 2, '0')
10 || la || lb || lc id
11 from a cross join b cross join c;
17576 rows created.
SQL>
Several sample values:
SQL> with temp as
2 (select id,
3 row_number() Over (order by id) rna,
4 row_Number() over (order by id desc) rnd
5 from test
6 )
7 select id
8 from temp
9 where rna <= 5
10 or rnd <= 5
11 order by id;
ID
----------
DJ01AAA
DJ02AAB
DJ03AAC
DJ04AAD
DJ05AAE
DJ99OUK
DJ99OUL
DJ99OUM
DJ99OUN
DJ99OUO
10 rows selected.
SQL>
However, if
It's ok to get like 20-30 unique entries, I think.
means that you need to generate at most 30 values, well, you'd easily create them manually; why would you develop any software solution for that? If you started typing, you'd be over by now.

How to insert into temp table when looping through a string - Oracle - PL/SQL

CREATE GLOBAL TEMPORARY TABLE tt_temptable(
RowNums NUMBER(3,0),
procNums NUMBER(18,0)
) ON COMMIT PRESERVE ROWS;
inputString VARCHAR2 ;
inputString := '12,13,14,15'
SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
FROM dual CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;
INSERT INTO tt_temptable(
SELECT identity(3) RowNums,procNums
FROM
);
Want to insert 12 , 13, 14 , 15 and identity of 3 length in the temptable so total 4 rows in temptable
If you use Oracle 12c, then you may define an IDENTITY column through GENERATED ALWAYS AS IDENTITY in your table definition and follow the way below :
SQL> CREATE GLOBAL TEMPORARY TABLE tt_temptable(
2 RowNums NUMBER(3,0) GENERATED ALWAYS AS IDENTITY,
3 procNums NUMBER(18,0)
4 ) ON COMMIT PRESERVE ROWS;
Table created
SQL>
SQL> DECLARE
2 inputString VARCHAR2(50) := '12,13,14,15';
3 BEGIN
4 INSERT INTO tt_temptable(procNums)
5 SELECT REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) ProcNums
6 FROM dual
7 CONNECT BY REGEXP_SUBSTR (inputString,'[^,]+',1,LEVEL) IS NOT NULL;
8 END;
9 /
PL/SQL procedure successfully completed
SQL> SELECT * FROM tt_temptable;
ROWNUMS PROCNUMS
------- -------------------
1 12
2 13
3 14
4 15
To reset the IDENTITY column (RowNums), use :
SQL> ALTER TABLE tt_temptable MODIFY( RowNums Generated as Identity (START WITH 1));
whenever the shared locks on the table are released.
insert
into tt_temptable
select NVL((select max(a.rownums)
from tt_temptable a
),100)+rownum
,procNums
from (SELECT REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) ProcNums,level as lvl
FROM dual
CONNECT BY REGEXP_SUBSTR ('10,20,30','[^,]+',1,LEVEL) IS NOT NULL
)x

How to secure table for avoid duplicate data

I cant resolve the problem how secure my table to avoid duplicate combination of attributes_positions. The best way to show you what I mean is the following image
column id_combination represents number of combination. Combination consists of attributes_positions. So Combination is sequence of attributes_positions.
And now I would secure table from insert exaclty the same sequence of attributes_positions.
Of course if already inserted combination contains one additional attributes_positions or one less than inserting combination is ok
image I show the different bettwen duplicate and not duplicate combination.
Is there a some way how I can do that?? Meaby something like 'before update'. But how to implement for this example. I`m not so pretty good with advanced sql.
The database where I trying to secure table is postgresql 9.4
I will be grateful for help
-- The data
CREATE TABLE theset (
set_id INTEGER NOT NULL PRIMARY KEY
, set_name text UNIQUE
);
INSERT INTO theset(set_id, set_name) VALUES
( 1, 'one'), ( 2, 'two'), ( 3, 'three'), ( 4, 'four');
CREATE TABLE theitem (
item_id integer NOT NULL PRIMARY KEY
, item_name text UNIQUE
);
INSERT INTO theitem(item_id, item_name) VALUES
( 1, 'one'), ( 2, 'two'), ( 3, 'three'), ( 4, 'four'), ( 5, 'five');
CREATE TABLE set_item (
set_id integer NOT NULL REFERENCES theset (set_id)
, item_id integer NOT NULL REFERENCES theitem(item_id)
, PRIMARY KEY (set_id,item_id)
);
-- swapped index is indicated for junction tables
CREATE UNIQUE INDEX ON set_item(item_id, set_id);
INSERT INTO set_item(set_id,item_id) VALUES
(1,1), (1,2), (1,3), (1,4),
(2,1), (2,2), (2,3), -- (2,4),
(3,1), (3,2), (3,3), (3,4), (3,5),
(4,1), (4,2), (4,4);
CREATE FUNCTION set_item_unique_set( ) RETURNS TRIGGER AS
$func$
BEGIN
IF EXISTS ( -- other set
SELECT * FROM theset oth
-- WHERE oth.set_id <> NEW.set_id -- only for insert/update
WHERE TG_OP = 'DELETE' AND oth.set_id <> OLD.set_id
OR TG_OP <> 'DELETE' AND oth.set_id <> NEW.set_id
-- count (common) members in the two sets
-- items not in common will have count=1
AND NOT EXISTS (
SELECT item_id FROM set_item x1
WHERE (x1.set_id = NEW.set_id OR x1.set_id = oth.set_id )
GROUP BY item_id
HAVING COUNT(*) = 1
)
) THEN
RAISE EXCEPTION 'Not unique set';
RETURN NULL;
ELSE
RETURN NEW;
END IF;
END;
$func$ LANGUAGE 'plpgsql'
;
CREATE CONSTRAINT TRIGGER check_item_set_unique
AFTER UPDATE OR INSERT OR DELETE
-- BEFORE UPDATE OR INSERT
ON set_item
FOR EACH ROW
EXECUTE PROCEDURE set_item_unique_set()
;
-- Test it
INSERT INTO set_item(set_id,item_id) VALUES(4,5); -- success
INSERT INTO set_item(set_id,item_id) VALUES(2,4); -- failure
DELETE FROM set_item WHERE set_id=1 AND item_id= 4; -- failure
Note: There should also be a trigger for the DELETE case.
UPDATE: added handling of DELETE
(the handling of deletes is not perfect; imagine the case where the last element from a set is removed)
My answer assumes that the target is without dupes, and that we want to insert a new set - which happens to be a duplicate. I choose the group of 4 with the id_comb of 1.
You would have to put the group of 4 into a staging table. Then, you have to pivot both staging and target horizontally - so that you get 5 columns named attr_pos1 to attr_pos5 (the biggest group in your example is 5). To pivot, you need a sequence number, which we get by using ROW_NUMBER(). That's for both tables, staging and target. Then, you pivot both. Then, you try to join pivoted staging and target on all 5 attr_pos# columns, and count the rows. If you get 0, you have no duplicates. If you get 1, you have duplicates.
Here's the whole scenario:
WITH
-- input section: a) target table, no dupes
target(id_comb,attr_pos) AS (
SELECT 2,1
UNION ALL SELECT 2,2
UNION ALL SELECT 2,3
UNION ALL SELECT 2,4
UNION ALL SELECT 3,1
UNION ALL SELECT 3,2\
UNION ALL SELECT 3,3
UNION ALL SELECT 3,4
UNION ALL SELECT 3,5
UNION ALL SELECT 4,1
UNION ALL SELECT 4,2
UNION ALL SELECT 4,3
)
,
-- input section: b) staging, input, would be a dupe
staging(id_comb,attr_pos) AS (
SELECT 1,1
UNION ALL SELECT 1,2
UNION ALL SELECT 1,3
UNION ALL SELECT 1,4
)
,
-- query section:
-- add sequence numbers to stage and target
target_s AS (
SELECT
ROW_NUMBER() OVER(PARTITION BY id_comb ORDER BY attr_pos) AS seq
, *
FROM target
)
,
staging_s AS (
SELECT
ROW_NUMBER() OVER(PARTITION BY id_comb ORDER BY attr_pos) AS seq
, *
FROM staging
)
,
-- horizontally pivot target, NULLS as -1 for later join
target_h AS (
SELECT
id_comb
, IFNULL(MAX(CASE seq WHEN 1 THEN attr_pos END),-1) AS attr_pos1
, IFNULL(MAX(CASE seq WHEN 2 THEN attr_pos END),-1) AS attr_pos2
, IFNULL(MAX(CASE seq WHEN 3 THEN attr_pos END),-1) AS attr_pos3
, IFNULL(MAX(CASE seq WHEN 4 THEN attr_pos END),-1) AS attr_pos4
, IFNULL(MAX(CASE seq WHEN 5 THEN attr_pos END),-1) AS attr_pos5
FROM target_s
GROUP BY id_comb ORDER BY id_comb
)
,
-- horizontally pivot staging, NULLS as -1 for later join
staging_h AS (
SELECT
id_comb
, IFNULL(MAX(CASE seq WHEN 1 THEN attr_pos END),-1) AS attr_pos1
, IFNULL(MAX(CASE seq WHEN 2 THEN attr_pos END),-1) AS attr_pos2
, IFNULL(MAX(CASE seq WHEN 3 THEN attr_pos END),-1) AS attr_pos3
, IFNULL(MAX(CASE seq WHEN 4 THEN attr_pos END),-1) AS attr_pos4
, IFNULL(MAX(CASE seq WHEN 5 THEN attr_pos END),-1) AS attr_pos5
FROM staging_s
GROUP BY id_comb ORDER BY id_comb
)
SELECT
COUNT(*)
FROM target_h
JOIN staging_h USING (
attr_pos1
, attr_pos2
, attr_pos3
, attr_pos4
, attr_pos5
);
Hope this helps ----
Marco
Interesting but not very useful solution by #wildplasser. I create script to insert sample data:
WITH param AS (
SELECT 8 AS max
), maxarray AS (
SELECT array_agg(i) as ma FROM (SELECT generate_series(1, max) as i FROM param) as i
), pre AS (
SELECT
*
FROM (
SELECT
*, CASE WHEN (id >> mbit) & 1 = 1 THEN ma[mbit + 1] END AS item_id
FROM (
SELECT *,
generate_series(0, array_upper(ma, 1) - 1) as mbit
FROM (
SELECT *,
generate_series(1,(2^max - 1)::int8) AS id
FROM param, maxarray
) AS pre1
) AS pre2
) AS pre3
WHERE item_id IS NOT NULL
), ins_item AS (
INSERT INTO theitem (item_id, item_name) SELECT i, i::text FROM generate_series(1, (SELECT max FROM param)) as i RETURNING *
), ins_set AS (
INSERT INTO theset (set_id, set_name)
SELECT id, id::text FROM generate_series(1, (SELECT 2^max - 1 FROM param)::int8) as id
RETURNING *
), ins_set_item AS (
INSERT INTO set_item (set_id, item_id)
SELECT id, item_id FROM pre WHERE (SELECT count(*) FROM ins_item) > 0 AND (SELECT count(*) FROM ins_set) > 0
RETURNING *
)
SELECT
'sets', count(*)
FROM ins_set
UNION ALL
SELECT
'items', count(*)
FROM ins_item
UNION ALL
SELECT
'sets_items', count(*)
FROM ins_set_item
;
When I call it with 8 (1024 - 2^8 rows for set_item) it run 21 seconds. It is very bad. When I off trigger it took less then 1 milliseconds.
My proposal
It is very interesting to use arrays in this case. Unfortunatelly PostgreSQL does not support foreighn key for arrays, but it may be done by TRIGGERs. I remove set_item table and add items int[] field for theset:
-- The data
CREATE TABLE theitem (
item_id integer NOT NULL PRIMARY KEY
, item_name text UNIQUE
);
CREATE TABLE theset (
set_id INTEGER NOT NULL PRIMARY KEY
, set_name text UNIQUE
, items integer[] UNIQUE NOT NULL
);
CREATE INDEX i1 ON theset USING gin (items);
CREATE OR REPLACE FUNCTION check_item_CU() RETURNS TRIGGER AS $sql$
BEGIN
IF (SELECT count(*) > 0 FROM unnest(NEW.items) AS u LEFT JOIN theitem ON (item_id = u) WHERE item_id IS NULL) THEN
RETURN NULL;
END IF;
NEW.items = ARRAY(SELECT unnest(NEW.items) ORDER BY 1);
RETURN NEW;
END;
$sql$ LANGUAGE plpgsql;
CREATE TRIGGER check_item_CU BEFORE INSERT OR UPDATE ON theset FOR EACH ROW EXECUTE PROCEDURE check_item_CU();
CREATE OR REPLACE FUNCTION check_item_UD() RETURNS TRIGGER AS $sql$
BEGIN
IF (TG_OP = 'DELETE' OR TG_OP = 'UPDATE' AND NEW.item_id != OLD.item_id) AND (SELECT count(*) > 0 FROM theset WHERE OLD.item_id = ANY(items)) THEN
RAISE EXCEPTION 'item_id % still used', OLD.item_id;
RETURN NULL;
END IF;
RETURN NEW;
END;
$sql$ LANGUAGE plpgsql;
CREATE TRIGGER check_item_UD BEFORE DELETE OR UPDATE ON theitem FOR EACH ROW EXECUTE PROCEDURE check_item_UD();
WITH param AS (
SELECT 10 AS max
), maxarray AS (
SELECT array_agg(i) as ma FROM (SELECT generate_series(1, max) as i FROM param) as i
), pre AS (
SELECT
*
FROM (
SELECT
*, CASE WHEN (id >> mbit) & 1 = 1 THEN ma[mbit + 1] END AS item_id
FROM (
SELECT *,
generate_series(0, array_upper(ma, 1) - 1) as mbit
FROM (
SELECT *,
generate_series(1,(2^max - 1)::int8) AS id
FROM param, maxarray
) AS pre1
) AS pre2
) AS pre3
WHERE item_id IS NOT NULL
), pre_arr AS (
SELECT id, array_agg(item_id) AS items
FROM pre
GROUP BY 1
), ins_item AS (
INSERT INTO theitem (item_id, item_name) SELECT i, i::text FROM generate_series(1, (SELECT max FROM param)) as i RETURNING *
), ins_set AS (
INSERT INTO theset (set_id, set_name, items)
SELECT id, id::text, items FROM pre_arr WHERE (SELECT count(*) FROM ins_item) > 0
RETURNING *
)
SELECT
'sets', count(*)
FROM ins_set
UNION ALL
SELECT
'items', count(*)
FROM ins_item
;
This variant run less than 1ms

value substitution/replacement in a string

I have a string x-y+z. The values for x, y and z will be stored in a table. Say
x 10
y 15
z 20
This string needs to be changed like 10-15+20.
Anyway I can achieve this using plsql or sql?
using simple Pivot we can do
DECLARE #Table1 TABLE
( name varchar(1), amount int )
;
INSERT INTO #Table1
( name , amount )
VALUES
('x', 10),
('y', 15),
('Z', 25);
Script
Select CAST([X] AS VARCHAR) +'-'+CAST([Y] AS VARCHAR)+'+'+CAST([Z] AS VARCHAR) from (
select * from #Table1)T
PIVOT (MAX(amount) FOR name in ([X],[y],[z]))p
An approach could be the following, assuming a table like this:
create table stringToNumbers(str varchar2(16), num number);
insert into stringToNumbers values ('x', 10);
insert into stringToNumbers values ('y', 20);
insert into stringToNumbers values ('zz', 30);
insert into stringToNumbers values ('w', 40);
First tokenize your input string with something like this:
SQL> with test as (select 'x+y-zz+w' as string from dual)
2 SELECT 'operand' as typ, level as lev, regexp_substr(string, '[+-]+', 1, level) as token
3 FROM test
4 CONNECT BY regexp_instr(string, '[a-z]+', 1, level+1) > 0
5 UNION ALL
6 SELECT 'value', level, regexp_substr(string, '[^+-]+', 1, level) as token
7 FROM test
8 CONNECT BY regexp_instr(string, '[+-]', 1, level - 1) > 0
9 order by lev asc, typ desc;
TYP LEV TOKEN
------- ---------- --------------------------------
value 1 x
operand 1 +
value 2 y
operand 2 -
value 3 zz
operand 3 +
value 4 w
In the example I used lowercase literals and only +/- signs; you can easily edit it to handle something more complex; also, I assume the input string is well-formed.
Then you can join your decoding table to the tokenized string, building the concatenation:
SQL> select listagg(nvl(to_char(num), token)) within group (order by lev asc, typ desc)
2 from (
3 with test as (select 'x+y-zz+w' as string from dual)
4 SELECT 'operand' as typ, level as lev, regexp_substr(string, '[+-]+', 1, level) as token
5 FROM test
6 CONNECT BY regexp_instr(string, '[a-z]+', 1, level+1) > 0
7 UNION ALL
8 SELECT 'value', level, regexp_substr(string, '[^+-]+', 1, level) as token
9 FROM test
10 CONNECT BY regexp_instr(string, '[+-]', 1, level - 1) > 0
11 order by lev asc, typ desc
12 ) tokens
13 LEFT OUTER JOIN stringToNumbers on (str = token);
LISTAGG(NVL(TO_CHAR(NUM),TOKEN))WITHINGROUP(ORDERBYLEVASC,TYPDESC)
--------------------------------------------------------------------------------
10+20-30+40
This assumes that every literal in you input string has a corrensponding value in table. You can even handle the case of strings with no corrensponding number, for example assigning 0:
SQL> select listagg(
2 case
3 when typ = 'operand' then token
4 else to_char(nvl(num, 0))
5 end
6 ) within group (order by lev asc, typ desc)
7 from (
8 with test as (select 'x+y-zz+w-UNKNOWN' as string from dual)
9 SELECT
.. ...
16 ) tokens
17 LEFT OUTER JOIN stringToNumbers on (str = token);
LISTAGG(CASEWHENTYP='OPERAND'THENTOKENELSETO_CHAR(NVL(NUM,0))END)WITHINGROUP(ORD
--------------------------------------------------------------------------------
10+20-30+40-0
Create a function like this:
create table ttt1
( name varchar(1), amount int )
;
INSERT INTO ttt1 VALUES ('x', 10);
INSERT INTO ttt1 VALUES ('y', 15);
INSERT INTO ttt1 VALUES ('z', 25);
CREATE OR REPLACE FUNCTION replace_vars (in_formula VARCHAR2)
RETURN VARCHAR2
IS
f VARCHAR2 (2000) := UPPER (in_formula);
BEGIN
FOR c1 IN ( SELECT UPPER (name) name, amount
FROM ttt1
ORDER BY name DESC)
LOOP
f := REPLACE (f, c1.name, c1.amount);
END LOOP;
return f;
END;
select replace_vars('x-y+z') from dual
Here's another way to approach the problem that attempts to do it all in SQL. While not necessarily the most flexible or fastest, maybe you can get some ideas from another way to approach the problem. It also shows a way to execute the final formula to get the answer. See the comments below.
Assumes all variables are present in the variable table.
-- First build the table that holds the values. You won't need to do
-- this if you already have them in a table.
with val_tbl(x, y, z) as (
select '10', '15', '20' from dual
),
-- Table to hold the formula.
formula_tbl(formula) as (
select 'x-y+z' from dual
),
-- This table is built from a query that reads the formula a character at a time.
-- When a variable is found using the case statement, it is queried in the value
-- table and it's value is returned. Otherwise the operator is returned. This
-- results in a row for each character in the formula.
new_formula_tbl(id, new_formula) as (
select level, case regexp_substr(formula, '(.|$)', 1, level, NULL, 1)
when 'x' then
(select x from val_tbl)
when 'y' then
(select y from val_tbl)
when 'z' then
(select z from val_tbl)
else regexp_substr(formula, '(.|$)', 1, level, NULL, 1)
end
from formula_tbl
connect by level <= regexp_count(formula, '.')
)
-- select id, new_formula from new_formula_tbl;
-- This puts the rows back into a single string. Order by id (level) to keep operands
-- and operators in the right order.
select listagg(new_formula) within group (order by id) formula
from new_formula_tbl;
FORMULA
----------
10-15+20
Additionally you can get the result of the formula by passing the listagg() call to the following xmlquery() function:
select xmlquery(replace( listagg(new_formula) within group (order by id), '/', ' div ')
returning content).getNumberVal() as result
from new_formula_tbl;
RESULT
----------
15

Custom sort with series of numbers(sequences) using oracle sql

I have a Table Abstract, which has one of the columns (SerialNumber). It is having data as below.
1
1.1
1.1.1
1.1.2
1.2
..
..
10
10.1
10.2
Now, my requirement is to sort the data based on this column as first preference.
Maximum of 2 "dots" are possible in a SerialNumber.
So 1.2.3.4 is not possible. Maximum Number may be 999 in any level of sequence.
ie. 999.999.999 is the maximum possible sequence.
I tried by issuing a ORDER BY SerialNumber, it comes like
1
10
10.1
..
2
2.1
Just because of character sort , Instead of 2, 10 comes after 1.
Any idea of how it can be achieved.? As I need this in JDBC and in multiple queries ( Different modules) hoping to have this as generic as possible.
I would probably use a regex function to split each part for ordering. Something like:
select serialnumber
from data
order by
to_number(regexp_substr(serialnumber, '[[:digit:]]+')),
to_number(regexp_substr(serialnumber, '[[:digit:]]+', 1, 2)) nulls first,
to_number(regexp_substr(serialnumber, '[[:digit:]]+', 1, 3)) nulls first
Which will give you results like:
SERIALNUMBER
-------------------------------
1.100
1.100.10
34.134.819
36
75.717
256.749.864
397
428.13.647
443
713.768
855.238
Function to extract every number using '.' as delimiter and lpad with 0s. And call the function() in order by
CREATE OR REPLACE FUNCTION FORMAT_MY_SERIAL(
ORIG_SERIAL VARCHAR2)
RETURN VARCHAR2
AS
FINAL_SERIAL VARCHAR2(15) := '';
SERIAL VARCHAR2(15);
BEGIN
SERIAL := ORIG_SERIAL;
WHILE (INSTR(SERIAL,'.') <> 0)
LOOP
FINAL_SERIAL := TO_CHAR(SUBSTR(SERIAL,INSTR(SERIAL,'.',-1)+1),'FM099')||'.'||FINAL_SERIAL;
SERIAL := SUBSTR(SERIAL,1,INSTR(SERIAL,'.',-1)-1);
END LOOP;
FINAL_SERIAL := TRIM(BOTH '.' FROM TO_CHAR(SERIAL,'FM099')||'.'||FINAL_SERIAL);
RETURN FINAL_SERIAL;
END FORMAT_MY_SERIAL;
/
And this is an example:
WITH MY_TABLE AS
( SELECT '1.1.1' AS SerialNumber FROM dual
UNION ALL
SELECT '10' FROM dual
UNION ALL
SELECT '1' FROM dual
UNION ALL
SELECT '1.2' FROM dual
UNION ALL
SELECT '2.1' FROM dual
UNION ALL
SELECT '1.10.1' FROM dual
UNION ALL
SELECT '2.1' FROM dual
)
SELECT SerialNumber,
FORMAT_MY_SERIAL(SerialNumber) as formatted
FROM MY_TABLE
ORDER BY FORMAT_MY_SERIAL(SerialNumber);
Result:
SERIAL FORMATTED
1 001
1.1.1 001.001.001
1.2 001.002
1.10.1 001.010.001
2.1 002.001
2.1 002.001
10 010
create table temp as select dummy from dual;
alter table temp add val varchar2(20);
select * from temp;
insert into temp (val) values ('1');
insert into temp (val)values ('1.2');
insert into temp (val)values ('1.1.1');
insert into temp (val)values ('2');
insert into temp (val)values ('2.1');
insert into temp (val)values ('10');
select val
,decode(substr(val,1,instr(val,'.')-1),null,val,substr(val,1,instr(val,'.')-1))
from temp
order by to_number(decode(substr(val,1,instr(val,'.')-1),null,val,substr(val,1,instr(val,'.')-1))),val