dynamic number of where condition in oracle sql - sql

I need to write a sql for a prompt in a reporting tool. I get the list of multiple values in a variable separated by '-' and the number of such values can vary.(eg1."abc-def" eg2."abc-def-xyz").
Now I need to write sql in oracle of this form(logically)
select something
from somewhere
where someColumn in 'abc' -- use substr to extract abc
or someColumn in 'def' -- use substr to extract def
or ...till we don't find '-'.
I can't use plsql for the query. Either I may not be knowing to use the variable in which I select into in plsql or may be the reporting tool doesn't support that.
Thanks in advance.

Try
select something
from somewhere
where someColumn in (select regexp_substr('abc-def-xyz','[^-]+', 1, level) from dual
connect by regexp_substr('abc-def-xyz', '[^-]+', 1, level) is not null);
To generalize (considering your fields are separated by "-")
select something
from somewhere
where someColumn in (select regexp_substr(variable,'[^-]+', 1, level) from dual
connect by regexp_substr(variable, '[^-]+', 1, level) is not null);
Basically the output of the subquery is shown below -
SQL> select regexp_substr('abc-def-xyz','[^-]+', 1, level) value from dual
connect by regexp_substr('abc-def-xyz', '[^-]+', 1, level) is not null;
VALUE
--------------------------------
abc
def
xyz

First split the string into its parts using Oracle's regexp_substr() function. See regexp_substr function (If you can access the original that generates the string, just use that.)
Second, put this in a temp table and then just have:
select something
from somewhere
where someColumn in (select parts from tmpTable)

select something
from somewhere
where INSTR('-'||'abc-def-ghi'||'-', '-'||someColumn||'-') > 0;

If you just want a list of results that contain a '-', you could just do something like
SELECT SOMETHING FROM SOMEWHERE WHERE SOMECOLUMN LIKE ('%-%');

Related

List of values as table

I'm looking for a smarter way to have a list of values as a table in Oracle.
What I do nowadays is
select 'value1' as val from dual
union
select 'value2' from dual
What I'm hoping for is some function/way/magic, that I'll do for example
select 'value1', 'value2' from dual -- + some additional magic
I'm looking for non-PL/SQL way which I think is overkill, but I'm not saying definite no to PL/SQL if that's the only option, but I can look here Create an Oracle function that returns a table for inspiration for PL/SQL. But extra table to have a list seems still easier to maintain than PL/SQL.
The motivation for not using select distict from transactional table is that I want to have a defined list of values, and with that approach, I can miss those I expect there but have no records in the table.
The expected number of elements in the list is several tens of records (like 30).
Here is one option:
select column_value
from table(sys.dbms_debug_vc2coll('value1', 'value2', 'value3', 'value4'));
Starting Oracle 12.2, you don't need the table function:
select column_value
from sys.dbms_debug_vc2coll('value1', 'value2', 'value3', 'value4');
Or yet another, similar:
SQL> select column_value
2 from table(sys.odcivarchar2list('Little', 'Foot', 'Scott', 'Tiger'))
3 order by column_value;
COLUMN_VALUE
----------------------------------------------------------------------------
Foot
Little
Scott
Tiger
SQL>
Starting with Oracle 12c you could use JSON_TABLE for that:
select *
from json_table('["value1", "value2"]', '$[*]'
columns val varchar(20) path '$');
If you aren't on 12c and can't use json_table (or even if you are/can but don't want to) you could use an XML sequence via xmltable instead:
select *
from xmltable('"value1", "value2", "value3"');
Result Sequence
--------------------------------------------------------------------------------
value1
value2
value3
I'd probably use an ODCI collection by default, but might be interesting to compare how all of these options compare with large data volumes - with 30 values you might not be able to see much of a difference.
Also, You can use the connect by query:
SQL> select regexp_substr('VALUE1,VALUE2','[^,]+', 1, level) from dual
2 connect by level <= regexp_count('VALUE1,VALUE2', '[^,]+');
REGEXP_SUBSTR('VALUE1,VALUE2','[^,]+',1,LEVEL)
----------------------------------------------------
VALUE1
VALUE2
SQL>

How many times will the "SELECT" inside "IN" execute?

I have less exposure to the performance aspect of Oracle. I want to know how exactly Oracle will handle this situation:
My procedure receives a comma-separated string, which I have to split based on comma and filter.
I know this works:
SELECT *
FROM some_table
WHERE some_attribute IN (SELECT
REGEXP_SUBSTR(comma_separated_string,'[^,]+', 1, LEVEL)
FROM DUAL
CONNECT BY REGEXP_SUBSTR(comma_separated_string,
'[^,]+', 1, LEVEL) IS NOT NULL);
Since, "REG_EX" is CPU intensive, I thought of replacing the above by:
SELECT *
FROM some_table
WHERE INSTR(','|| comma_separated_string||',', ','|| some_attribute||',') >
0;`
OR
SELECT *
FROM some_table
WHERE ','||comma_separated_string||',' like '%,'|| some_attribute ||',%';`
So, picking either one depends on how the select executes or, to be precise, how many times the clause after where executes.

SQL query to fetch rows based on a column

I have a table Employee as below. The Change column contains names of columns whose values are modified through my application. Data in this column is separated by comma. I need to query this table in such a way that the result will have one change per row. ie split the data in the Change column by comma and fetch the corresponding row. I don't have an idea where to start ! Please help.
Let's see, you could use Oracle's regexp_substr function:
select distinct Id, Name, Address, trim(regexp_substr(Change,'[^,]+', 1, level))
from Employee
connect by regexp_substr(Change, '[^,]+', 1, level) is not null;
This should work for any number of comma-separated values in your Change column.
See doc on the rexexp_substr function here: https://docs.oracle.com/cd/B12037_01/server.101/b10759/functions116.htm
here i have tried with the use of regexp_substr included multiset level
with temp as
(
select id, name, address, change from testemp
)
select id,name,address,trim(regexp_substr(change, '[^,]+', 1, levels.column_value)) change
from temp t,
table(cast(multiset(select level from dual
connect by level <= length (regexp_replace(change, '[^,]+')) + 1)
as sys.OdciNumberList)) levels;

Search and extract data from SQL column having some delimiter

I have column in table having data in below format:
(DeliveryMethod+NON;Installation_Method+NoInstallation;Services_Reference_ID+100118547,44444,33333;Service_ID+2222)
(key+value;key+value;key+value;key+value;key+value;key+value;key+value;)
I want to search and extract a particular "value" from this column based on specific "key" and "key+value" can be on any position, how to do this using a SQL query?
Here's one way to approach it in Oracle as I answered in this post: Oracle 11gR2: split string with multiple delimiters(add). Hopefully you can apply the logic to your RDBMS. Note that this answer doesn't just get the value from the string, but attempts to parse the string and return values so they can be processed like rows in a query's result set. This may be overkill for your scenario. At any rate, it's just one way to look at it.
-- Original data with multiple delimiters and a NULL element for testing.
with orig_data(str) as (
select 'DeliveryMethod+NON;Installation_Method+NoInstallation;;Services_Reference_ID+100118547,44444,33333;Service_ID+2222' from dual
),
--Split on first delimiter (semi-colon)
Parsed_data(rec) as (
select regexp_substr(str, '(.*?)(;|$)', 1, LEVEL, NULL, 1)
from orig_data
CONNECT BY LEVEL <= REGEXP_COUNT(str, ';') + 1
)
-- For testing-shows records based on 1st level delimiter
--select rec from parsed_data;
-- Split the record into columns
select regexp_replace(rec, '^(.*)\+.*', '\1') col1,
regexp_replace(rec, '^.*\+(.*)', '\1') col2
from Parsed_data;
Result:
To specifically answer your question, in order to get a value based on a key, change the last query to this in order to get the value where the key is 'Service_ID':
select value
from (
select regexp_replace(rec, '^(.*)\+.*', '\1') key,
regexp_replace(rec, '^.*\+(.*)', '\1') value
from Parsed_data )
where key = 'Service_ID';
Result:
Or to just extract it out of the string using a regular expression:
with orig_data(str) as (
select 'Service_ID+2222;DeliveryMethod+NON;Installation_Method+NoInstallation;;Services_Reference_ID+100118547,44444,33333' from dual
)
select regexp_substr(str, '(.*?)Service_ID\+(.+?)(;|$)', 1, 1, NULL, 2) value
from orig_data;

Oracle- create a temporary resultset for use in a query

How do I create a temporary result set for use in an SQL without creating a table and inserting the data?
Example: I have a list of, say 10 codes for example. I want to put this into a query, and then query the database to see which codes in this temporary list do not exist in a table.
If it was already in a table, I could do something like:
SELECT
ITEM_CODE
FROM
TEMP_ITEMS
MINUS
SELECT
ITEM_CODE
FROM
M_ITEMS
Is their a way without using PL/SQL, and pure SQL to create a temporary rowset before querying?
Please don't answer with something like:
SELECT 1 FROM DUAL
UNION ALL
SELECT 2 FROM DUAL
I am sort of thinking of something where I can provide my codes in an IN statement, and it turns that into rows for use in a later query.
Edit: so everyone knows my objective here, basically I sometimes get a list of product codes that I need to find which ones in the list are not setup in our system. I want a quick way to throw this into an SQL statement so I can see which ones are not in the system (rather than importing data etc). I usually put these into excel, then do a formula such as :
="'"&A1&"',"
So that I can create my comma separated list.
If you are using oracle 11g you can do this
with t as
(
select (column_value).getnumberval() Codes from xmltable('1,2,3,4,5')
)
SELECT * FROM t
WHERE NOT EXISTS (SELECT 1 FROM M_ITEMS M WHERE codes = M.ITEM_CODE);
or
with t as
(
select (column_value).getstringval() Codes from xmltable('"A","B","C"')
)
SELECT * FROM t
WHERE NOT EXISTS (SELECT 1 FROM M_ITEMS M WHERE codes = M.ITEM_CODE);
I would go with:
with t as (
select 1 as val from dual union all
select 2 as val from dual
)
select . . .
And then use "t" or whatever you call it, in the subsequent query block.
I'm not sure what the objection is to using the select method . . . just pop the values you want in a column in Excel and produce the code for each value by copying down the formula. Then paste the results back into your query interface.
If you want to use a temporary table, you can use the values clause. Alternatively, you can use string functions if you only want IN functionality. Put the values in a comma separated list and check to see if it matches a particular value:
where ','||<list>||',' like '%,'||col||',%'
This one is interesting because it's not a union and fit in a single select. You have to enter the string with delimiters ('a/b/c/def') two times though:
SELECT regexp_substr('a/b/c/def', '[^/]+', 1, ROWNUM) var,
regexp_substr('2/432/sd/fsd', '[^/]+', 1, ROWNUM) var2
FROM dual
CONNECT BY LEVEL <= length(regexp_replace('a/b/c/def', '[^/]', '')) + 1;
var var2
=== ====
a 2
b 432
c sd
def fsd
Note: Credits go to : https://stackoverflow.com/a/1381495/463056
So using the with clause it would give someting like :
with tempo as (
SELECT regexp_substr('a/b/c/def', '[^/]+', 1, ROWNUM) var,
regexp_substr('2/432/sd/fsd', '[^/]+', 1, ROWNUM) var2
FROM dual
CONNECT BY LEVEL <= length(regexp_replace('a/b/c/def', '[^/]', '')) + 1
)
select ...
or you can use it in a from clause :
select ...
from (
SELECT regexp_substr('a/b/c/def', '[^/]+', 1, ROWNUM) var,
regexp_substr('2/432/sd/fsd', '[^/]+', 1, ROWNUM) var2
FROM dual
CONNECT BY LEVEL <= length(regexp_replace('a/b/c/def', '[^/]', '')) + 1
) tempo
There are two approaches I would lean towards:
1. Global Temporary Table
Although you say you don't want to create a table, it depends on why you don't want a table. If you choose to create a Global Temporary table, the rows are only visible to the session that inserted them, so it's like having a private in-memory table but gives you all the benefits of a real table - i.e. being able to query and join to it.
2. Pipelined function
You can create a function that returns the results in a form that can be queried using the TABLE() operator. More info here: http://www.oracle-base.com/articles/misc/pipelined-table-functions.php
It's a bit hokey-looking. But you can parse a string into separate rows using regular expressions assuming you are using 10g or later. For example
SQL> ed
Wrote file afiedt.buf
1 SELECT REGEXP_SUBSTR('a,b,c,def,g', '[^ |,]+', 1, LEVEL) parsed_str
2 FROM dual
3* CONNECT BY LEVEL <= REGEXP_COUNT('a,b,c,def,g', '[^ |,]+')
SQL> /
PARSED_STR
--------------------------------------------
a
b
c
def
g
Personally, I would find a pipelined table function or a PL/SQL block that generates a collection easier to understand, but if you have to do it in SQL you can.
Based on your edit, if you are getting a list of product codes that is already in some sort of file, it would seem to make more sense to use an external table to expose the file as a table or to use SQL*Loader to load the data into a table (temporary or permanent) that you can query. Barring either of those options, if you really want to manipulate the list in Excel first, it would make more sense to generate an IN list in Excel and just copy and past that into your query. Generating a comma-separated list of codes in Excel only to parse that list into it's constituent elements in SQL seems like way too many steps.