I am trying to pull two tables from an Oracle SQL database, and want to join them sequentially, so they appear as if they are one list.
List one has items [1,2,3,4]
List two has items [a,b,c,d]
I want to output [1,2,3,4,a,b,c,d]
Any thoughts?
One option uses a UNION with a computed column:
SELECT val
FROM
(
SELECT val, 1 AS position FROM table1
UNION ALL
SELECT val, 2 AS position FROM table2
) t
ORDER BY
position, val;
Demo
Note that I assume that all data here is text. If not, e.g. the first table be numeric, then we would have to do a cast along the way. But, this is not the main focus of your question anyway.
SELECT id_1, value_column1 from table_1
UNION
SELECT id_2, value_column2 from table_2;
if the types of columns are different - make sure you cast/convert them to char() - the resulting type should be same.
https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries004.htm
use union, i think 1,2,3 as numeric value that why converted it on varchar as for union you have to same data type
with t1 as (
select 1 as id from dual union all
select 2 from dual union all
select 3 from dual union all
select 4 from dual
), t2 as (
select 'a' as item from dual union all
select 'b' from dual union all
select 'c' from dual union all
select 'd' from dual
)
select cast(id as varchar(20)) as id from t1
union
select * from t2
demo
output
1
2
3
4
a
b
c
d
Related
I have one table with two columns in it. First one is 'tourist_name' and second one is 'visited_places'. So I need a query which will give me following output :
'tourist_name' and the places which is not visited by tourist.
You can generate the rows using cross-join and then simply use minus clause to get your desired result -
WITH TAB AS (SELECT 1 TOURIST_NAME, 'B' VISITED_PLACE FROM DUAL UNION ALL
SELECT 1, 'C' FROM DUAL UNION ALL
SELECT 1, 'D' FROM DUAL UNION ALL
SELECT 2, 'A' FROM DUAL UNION ALL
SELECT 2, 'E' FROM DUAL)
SELECT TOURIST_NAME, VISITED_PLACE FROM (SELECT DISTINCT VISITED_PLACE FROM TAB),
(SELECT DISTINCT TOURIST_NAME FROM TAB)
MINUS
SELECT TOURIST_NAME, VISITED_PLACE FROM TAB;
Link.
Suppose I have the following query :
select value from table where value in ('abc','cde','efg');
If only 'abc' is populated in the table,
I want to be able to see which value is missing in the result set,
so the results looks like :
cde
efg
You can use UNION ALL to get a resultset with all the values that you want:
SELECT 'abc' AS value FROM dual UNION ALL
SELECT 'cde' FROM dual UNION ALL
SELECT 'efg' FROM dual
(you may omit FROM dual depending on your database).
And with NOT EXISTS get all the values from the above resultset that do not appear in the table:
SELECT u.*
FROM (
SELECT 'abc' AS value FROM dual UNION ALL
SELECT 'cde' FROM dual UNION ALL
SELECT 'efg' FROM dual
) u
WHERE NOT EXISTS (SELECT 1 FROM tablename t WHERE t.value = u.value)
In oracle is it possible to join a static list to a table?
The list I have is something like this
ID
1
2
3
4
5
6
I don't want to create a table for this list
But then I want to join the list to an existing table that has the ID's in it... hoping to do a left join with the list
Is this possible?
You are lookig for a WITH clause that contains UNIONs of SELECT FROM DUAL.
Like :
WITH my_list AS (
select 'A' my_value from dual
UNION ALL select 'B' my_value from dual
UNION ALL select 'C' my_value from dual
)
SELECT
*
FROM
my_list
LEFT JOIN my_table ON my_table.my_field = my_list.my_value
;
You can generate the ID list in a CTE and then join it to whatever you want.
with id_list as (
select rownum as id
from dual
connect by level <= 6
)
select * from id_list;
ID
1
2
3
4
5
6
https://livesql.oracle.com/apex/livesql/s/hm2mczgx5udiig9vhryo86mfm
As the title says, I'm trying to add an extra column to a table which autoincrements everytime a different string in another column changes.
I would like to do this in a query.
Example:
MyCol GroupID
Cable 1
Cable 1
Foo 2
Foo 2
Foo 2
Fuzz 3
Fizz 4
Tv 5
Tv 5
The GroupID column is what I want to accomplish.
We can be sure that MyCol's strings will be the same in each subgroup (Foo will always be Foo, etc).
Thanks in advance
If I understand correctly, you can use dense_rank():
select t.*, dense_rank() over (order by col1) as groupid
from t;
You could make a temporal table with the distinct value of the MyCol and get the groupId throught the RowNumber of the temp table, and join the rownumbered result with your table.
This is a raw example in oracle:
WITH data AS
(SELECT 'Cable' MyCol FROM dual
UNION ALL
SELECT 'Cable' FROM dual
UNION ALL
SELECT 'Foo' FROM dual
UNION ALL
SELECT 'Foo' FROM dual
UNION ALL
SELECT 'Foo' FROM dual
UNION ALL
SELECT 'Fuzz' FROM dual
UNION ALL
SELECT 'Fizz' FROM dual
UNION ALL
SELECT 'Tv' FROM dual
UNION ALL
SELECT 'Tv' FROM dual
),
tablename AS
(SELECT * FROM data
),
temp AS
( SELECT DISTINCT mycol FROM tablename
),
temp2 AS
( SELECT mycol, rownum AS groupid from temp
)
SELECT tablename.mycol, temp2.groupid FROM temp2 JOIN tablename ON temp2.mycol = tablename.mycol
You could also check for a way to implement the tabibitosan method knowing that your column condition is string.
I need to convert the result of the below query into row output.
select 'Purchase','Sale','Discount','Out of Stock' from dual
Output:
Purchase
Sale
Discount
Out of Stock
You have to use UNPIVOT to get it. UNPIVOT is opposite of PIVOT and it converts column values to
with tbl(col1,col2,col3,col4) as
(
select 'Purchase','Sale','Discount','Out of Stock' from dual
),tbl2 as(
SELECT *
FROM tbl UNPIVOT (dat for col in (col1,col2,col3, col4)))
select dat from tbl2
If you have 1 more row, then it is better to populate the column names also like below.
with tbl(col1,col2,col3,col4) as
(
select 'Purchase','Sale','Discount','Out of Stock' from dual union
select 'foo','bar','data','blah' from dual
)
SELECT *
FROM tbl UNPIVOT (dat for col in (col1,col2,col3, col4));
But if you just want the values, then select only 'dat' column as in first example.
You could use UNION to have the values as different rows. For example,
SQL> WITH DATA(item) AS
2 ( SELECT 'Purchase' FROM dual
3 UNION
4 SELECT 'Sale' FROM dual
5 UNION
6 SELECT 'Discount' FROM dual
7 UNION
8 SELECT 'Out of Stock' FROM dual
9 )
10 SELECT item FROM DATA;
ITEM
------------
Discount
Out of Stock
Purchase
Sale
SQL>
Remember, union doesn't allow duplicates, so use UNION ALL if you want to allow duplicate rows.