input string to table - sql

I am doing some debugging in SQL for oracle 10g. I have a big input string which is used in "IN Clause" i.e.
select * from table where col in ('str2','str3','str4','str5',...)
i want to convert the in clause to rows or table?
Is there a way to do this i.e.
select 'str2','str3','str4','str5', .. from dual
but this outputs multiple columns and i want multiple rows?
Edit:
Here is what i am trying to do. suppose i have an excel data in tmp_table1 (cant create in reality) and tmp_table1 is same as the IN clause, then the below statement will give the missing keys.
SELECT *
FROM tmp_table1
WHERE unique_id NOT IN (
SELECT unique_id
FROM table1
WHERE unique_id IN
('str1', 'str2', 'str3', 'str4'))
now #andriy-m solution works if the in string is less than 4000. but what if its greater?

You are probably looking for this solution.

You can UNION the values into multiple rows:
SELECT 'str2' AS col FROM dual
UNION
SELECT 'str3' FROM dual
UNION
SELECT 'str4' FROM dual
UNION
SELECT 'str5' FROM dual

Related

query with LEAST returning multiple rows in oracle

When I ran the below query in Oracle 11g
SELECT least(val)
FROM
(
SELECT 1 AS val
FROM dual
UNION
SELECT 2 AS val
FROM dual
UNION
SELECT 3 AS val
FROM dual
);
I was expecting a single row but it is returning multiple rows. please help me out where exactly my understanding is going wrong..
Oracle's LEAST function returns the least value in a list of expressions, e.g. LEAST(1, 2, 3) would return 1. So LEAST could be used to find the minimum value across a collection of columns, e.g. LEAST(col1, col2, col3). What you are seeing is to be expected, i.e. you are getting back three records with the smallest value of each record.
Instead, if you want the minimum over an aggregate of rows, then you should be using MIN, e.g.
select min(val)
from
(
select 1 as val from dual union all
select 2 from dual union all
select 3 from dual
);

The purpose of 'value' keyword in Oracle Select Statement

Whats is the difference between:
select 'mari makan' value from dual ;
and
select 'mari makan' from dual ;
when querying from oracle table.
Precisely, what is the purpose of value keyword in above statement?
The first names the column value. It is more typically written using as:
select 'mari makan' as value
from dual ;
The second does not give a user-defined name to the column.
The difference between them is that the first query aliasing this column, and giving it a unique name, while the other one doesn't.
What does that mean?
SELECT * FROM (
SELECT 'asda' FROM dual
UNION
SELECT 'asdasda' FROM dual
)
WHERE ??? = ??
This query is not aliasing the column, if you would want to use this column in the other query, you will have a problem. I think the default is to name the column after what you selected, which means it will be named 'asda' .
As opposed to:
SELECT * FROM (
SELECT 'asda' Col1 FROM dual
UNION
SELECT 'asdasda' as Col1 FROM dual
) WHERE Col1 <> 'asda'
Which will let you call this column from an outer query.
The standard is to write it as SELECT <Col> AS <New_Name> but it can be written with out the AS as well.

Oracle Query fetching table name alongwith column name

select trx_id,refernce number from
(select * from abcd_1_txt union
select * from abcd_2_txt union
select * from abcd_3_txt union
select * from abcd_4_txt)
where trx_id in (123,321,1234)
In the query all the tables are of same format, same column names and same number of columns.
After running this query, surely i will get some data.
My question --- is there any way to know from which of these tables, i am getting the output.
Try to add a column with number of query as below
select qrynum, trx_id,refernce number from
(select 1 as qrynum,* from abcd_1_txt union
select 2,* from abcd_2_txt union
select 3,* from abcd_3_txt union
select 4,* from abcd_4_txt)
where trx_id in (123,321,1234)
as Joe W said in the comment below you can also use name of the table instead of query number, short example:
select tabname, trx_id,refernce number from
(select 'abcd_1_txt' as tabname,* from abcd_1_txt union
...
where trx_id in (123,321,1234)
but both ways don't eliminate duplicates, so you can use union all instead of union. Other way to do that is to run quires separately with the condition
select * from abcd_1_txt where trx_id in (123,321,1234)
select * from abcd_2_txt where trx_id in (123,321,1234)
.
.
.

Using Union with Insert to add one row

I have a query which is made up of two select statements with a union in the middle.
This works for what I need it for.
However, there is one value missing which I want to manually enter.
What I'm looking to is:
select * from tab1
union
select * from tab2
union
insert values('John',cast('2013-01-01' as date), 'Jim', 130)
Unfortunately this is not working. Can someone suggest how I do this please?
I'm using Teradata.
You need to keep selecting:
select * from tab1
union
select * from tab2
union
select 'John', cast('2013-01-01' as date), 'Jim', 130 from dual
The name dual is used in Oracle for a table with one row (and one column). Depending on the DBMS you use, you may be able to omit that final FROM altogether (and you may be able to do this in Oracle too):
select * from tab1
union
select * from tab2
union
select 'John', cast('2013-01-01' as date), 'Jim', 130
or you may have to choose from a system catalog table and ensure you get one row returned (FROM systables WHERE tabid = 1 was the classic mechanism in Informix, though you could also use 'sysmaster':sysdual instead of dual, etc, too), or you can select from any other table with a query that is guaranteed one row. There are probably ways to do it using a VALUES clause too.
Note the change from double quotes to single quotes. In strict standard SQL, double quotes enclose a delimited identifier, but single quotes surround strings.
From your question I' guessing you just want to SELECT that row, not INSERT it (into the database):
select * from tab1
union
select * from tab2
union
select "John", cast('2013-01-01' as date), "Jim", 130
You just want to SELECT the data, not INSERT it.
Not very familiar with TeraData, perhaps you need a FROM in which case limiting to 1 record would also make sense:
select * from tab1
union
select * from tab2
union
SELECT 'John',cast('2013-01-01' as date), 'Jim', '130' FROM dbc.columns 1
I am affraid the union must reference a table and if you need to add the data that does not exist in your database, try this:
select * from tab1
union
select * from tab2
union
select * from (SELECT 'John' as col1 ,cast('2013-01-01' as date) as col2, 'Jim' as col3, '130' as col4) dummy
You will, of course, have to change the name of the columns to fit those from your db (i.e. dont use col1, col2 etc.).
Good luck!

Column names for a table formed by a UNION

Given a couple of simple tables like so:
create table R(foo text);
create table S(bar text);
If I were to union them together in a query, what do I call the column?
select T.????
from (
select foo
from R
union
select bar
from S) as T;
Now, in mysql, I can apparently refer to the column of T as 'foo' -- the name of the matching column for the first relation in the union. In sqlite3, however, that doesn't seem to work. Is there a way to do it that's standard across all SQL implementations?
If not, how about just for sqlite3?
Correction: sqlite3 does allow you to refer to T's column as 'foo' after all! Oops!
Try to give an alias to columns;
select T.Col1
from (
select foo as Col1
from R
union
select bar as Col1
from S) as T;
or If the name of column is not necessary then T.* will be enough.
Although there is no spelled rule, we can use the column names from the first subquery in the union query to fetch the union results.
you only need column aliases only in first select (tested in SQl Server 2008 R2)
select T.Col1
from (
select 'val1' as Col1
union
select 'val2'
union
select 'val3'
) as T;