Column Row Transpose in Oracle Sql - oracle

Hi I have a simple query which give this result
And I want to modify it as follows. the Name column becomes the column headers and the studentID column becomes the 1st row

WITH t AS
(SELECT 1001 studentid, 'john' NAME FROM dual
UNION ALL
SELECT 1002, 'kane' FROM dual
)
SELECT * FROM (
SELECT studentid, NAME FROM t)
pivot (max(studentid) for name in ('john' John, 'kane' Kane));

Related

How to load the values of three column from one table1 to another table 2 which is having single column in hive?

Could you please help me below query.
Table 1 (Input)had three columns (Credit_date, Debit_date, Payment_date) and
Table 2 (Output) has one column date
The three column values from Table 1 should be available in Table 2.
I tried below query but not working.
insert into table2
select date
from (
(select credit_date, debit_date, Payment_date from table 1) as date)t;
Could you please guide.
Try doing a union all:
insert into table2
select * from (
select credit_date as date from table1
union all
select debit_date as date from table1
union all
select payment_date as date from table1
) t

Inserting from a SELECT but changing one column in the middle?

Wondering if there is a way to insert a row into a table from another, with exception of one column in the middle without specifying all the column name? I have 128 columns in the table.
I created a view to store the original records.
CREATE VIEW V_TXN_STG AS
SELECT * FROM TXN_STG;
In table TXN_STG, only one column BRN_CODE is changing.
Something like this doesn't work, because the column is not on the last, but somewhere middle of table structure.
INSERT INTO TXN_STG
SELECT v.*, 'BRN-001' AS BRN_CODE
FROM V_TXN_STG v;
I don't believe that this is possible without explicitly specifying the columns in your select.
first you have to get the columns:
SELECT listagg(column_name, ',') within group (order by column_name) columns
FROM all_tab_columns
WHERE table_name = 'AAA' --Table to insert too
and column_name <> 'B' -- column name you want to exclude
GROUP BY table_name;
Then insert that result on the first row:
insert into aaa(A,C) -- A,C is my result from above,I have excluded column B
select *
from (select 'a' A,'q' AMOUNT,'c' C from dual union all -- my sample data
select 'a','a','c' from dual union all
select 'a','b','c' from dual union all
select 'a','c','c' from dual union all
select 'a','d','c' from dual )
pivot
(
max(1)
for (AMOUNT) -- the column you want to remove from the sample data
IN ()
)
where 1=1
order by A;

How to insert data from multi tables to one table in Oracle?

I'd a trouble with inserting data from 3 table:
A (id, name), B (id, name), C (id, name). They have the same field like that.
How can I insert data from 3 tables above into table D (id, name)?
You could use UNION or UNION ALL
INSERT INTO table_d(id, name)
SELECT id, name
FROM table_a
UNION ALL
SELECT id, name
FROM table_b
UNION ALL
SELECT id, name
FROM table_c;
If you want to remove duplicate rows in 3 tables, change UNION ALL to UNION. Refer information about union vs union all

Concat Results of 2 Select Queries into 1 Column (oracle)

Im trying to insert a record into my table. But there is 1 column in which I want to get concatenated results of 2 select statements. Like the 2 statements will fetch their records and concatenate to form 1 value so that it can be inserted into the column.
insert into ABC (Name,City,Age)
Values ('John',(
(Select City from TableA where ID=1)concat(Select City from TableA where ID=2)),'22')
Or it can be comma separated but I am not getting what to use here.
Try this one:
INSERT INTO ABC (Name, City, Age)
VALUES ('John',
(
(SELECT City FROM TableA WHERE ID = 1) ||
(SELECT City FROM TableA WHERE ID = 2)
),
'22');
But ensure ... WHERE ID = 1 and ....WHERE ID = 2 return one row.
Using a cross join to select from the two tables produces a nice clear statement:
insert into ABC (Name,City,Age)
select 'John', concat(t1.city, t2.city), 22
from TableA t1
cross join TableA t2
where t1.ID = 1
and t2.ID = 2
/
Use CONCAT() or CONCAT_WS() functions for this (reference)
insert into ABC (Name,City,Age) Values (
'John',
CONCAT_WS(' ', (Select City from TableA where ID=1), (Select City from TableA where ID=2)),
'22'
)

how to get exact string value from delimited column value match in oracle database

I have 2 tables
Table 1:
#######
ID Location
1 India
2 Australia
Table 2:
############
Name Locations
test1 India|North America
test2 Indiana|Australia
I used the below query to get the Name from table 2 if it contains Location in Locations of table 2.
select Name
from table2 t2 inner join table1 t1
on instr(t1.Location,t2.Locations,length(t1.Location)) >= 1;
But when executed it still gives me results for Indiana as well whereas it should just return me result for location India alone.
I tried using contains in query too, but contains takes second parameter as string but not as column name.
Is there any other approach on this?
regexps always help in such cases
with
table1 (id, location) as (
select 1, 'India' from dual union
select 2, 'Australia' from dual
),
table2 (name, locations) as (
select 'test1', 'India|North America' from dual union
select 'test2', 'Indiana|Australia' from dual
)
select *
from table2 join table1 on
regexp_like (locations, '(^|\|)' || location || '(\||$)')
Try to look up location with delimiter, like this:
select Name from table2 t2
inner join table1 t1 on instr(t2.Locations,t1.Location||'|') >= 1

Resources