Populate data from one column to another column on condition in hive - hadoop

WE have :-
TABLE_1 having column (A_COL,NUM ) and TABLE_2 having column ( B_COL ,C_COL , NUM)
Q condition
If A_COL is NULL in the TABLE_1 then, populate the C_COL into B_COL column from TABLE_2
TRIED :-
Select
CASE WHEN A_COL IS NULL THEN B_COL = C_COL end as ABC_COL
from line
FROM TABLE_2 A
LEFT OUTER JOIN
TABLE_1 B
ON
A.NUM = B.NUM
I tried this but B_COL = C_COL not able to populate column C_COL into B_COL ,Do we have any other way to do this?

Try the below :
tab_1 as A and tab_2 as B
Selecting complete table 2 with condition :
Select
B.colB,
if ( A.colA = null , B.colB , B.colC) as B.colC,
B.num
from Tab_2 B
LEFT OUTER JOIN
TABLE_1 A
ON
A.NUM = B.NUM
Let me know if it dint work out.

Related

OR in Select Oracle

select * from table1 t1 where t1.column1 = 'someValue' and ((t1.column2 =1) OR (sysdate < select t1.DateColumn2 + t2.DateColumn2/1440
from
table2 t2 where t1.column3 = t2.column3));
if t1.column2 =1 evaluates to false, I want to check another condition if time t1.DateColumn2 + t2.DateColumn2 is < sysdate . Oracle is throwing syntax error near or condition. Cant sysdate be used directly like that? Not sure where I am going wrong. Thanks
If I am guessing your intention correctly, you want an exists clause
select *
from table1 t1
where t1.column1 = 'someValue'
and ( (t1.column2 =1)
OR exists( select 1
from table2 t2
where t2.column3 = t1.column3
and sysdate < t1.DateColumn2 + t2.DateColumn2/1440 ));
Or just join the two tables in the outer query assuming there is at most 1 row in t2 per t1 row (if there is exactly 1 row you should do an inner join rather than a left outer join)
select t1.*
from table1 t1
left outer join table2 t2
on( t1.column3 = t2.column3 )
where t1.column2 = 1
or sysdate < t1.DateColumn2 + t2.DateColumn2/1440;

oracle insert into column using subquery

I want to insert data into a column in the table.
Table a
ID col1 col2
1 A null
2 B null
Table b
ID col1
1 C
2 D
Expected results:
Table A
ID col1 col2
1 A C
2 B D
I tried this:
insert into tableA (col2)
select b.col1
from tableB b , tableA a
where b.id = a.id
and I received
0 row inserted.
How do I insert the col1 in B into col2 in A for the matching 'id' columns?
Thank you.
You must use Merge statement when inserting based on joins.
Also in table tableA col2 already exist but you want to insert a value on join then you must update that column.
merge into tablea a
using tableb b
on (b.id = a.id)
when matched
then
update set a.col2 = b.col1;
What you want to do shouldn't require a subquery. I'm not a huge fan of the table a, table b notation, try this:
update a
set col2 = b.col1
from tableB b
join tableA a
on a.id = b.id

Use a value of a query in a subquery

I'm trying to make a query that have a lot of subquerys (4 subquerys), but for the subquerys I need to take a value of the query, can anyone help me? This is the query:
SELECT DISTINCT tab1.value1,
ISNULL((SELECT SUM(tab2.quantity) FROM tab2 INNER JOIN tab3 ON tab2.id_tab2 = tab3.id_tab2 INNER JOIN tab4 ON tab3.id_tab3 = tab4.id_tab3 WHERE tab4.value = "value1"), 0) AS v1,
ISNULL((SELECT SUM(tab2.quantity) FROM tab2 INNER JOIN tab3 ON tab2.id_tab2 = tab3.id_tab2 INNER JOIN tab4 ON tab3.id_tab3 = tab4.id_tab3 WHERE tab4.value = "value2"), 0) AS v2
What I need to do is that in the subquerys, make an INNER JOIN with that value1 the subquery make the adding but just with the values that are of the value1, because right now the subquery make the add of all the values located in the table that are equal with "value1" or "value2", I have tryed to make a subquery like these:
ISNULL((SELECT SUM(tab2.quantity) FROM tab2 INNER JOIN tab3 ON tab2.id_tab2 = tab3.id_tab2 INNER JOIN tab4 ON tab3.id_tab3 = tab4.id_tab3 INNER JOIN tab5 ON tab2.id_tab5 = tab5.id_tab5 INNER JOIN tab1 ON tab5.id_tab1 = tab1.id_tab1 INNER JOIN tab1 ON tab1.value1 = tab1.value1 WHERE tab4.value = "value1"), 0) AS v1
But obviously it didn't function, I also have tried to use an allias for the value1 but it says that it doesn't exist, anyone have an idea?
Im going to take a stab at it... It looks like you are trying to join the four tables by _id and aggregate a sum based on the child table's (tab4) value.
In the below example I used three tables, but the idea is the same. If this doesnt meet your needs perhaps you can repurpose this example to clearly define your requirements:
declare #tab1 table (i int primary key, value varchar(10));
insert into #tab1
select 1, 'one' union all select 2, 'two'
declare #tab2 table (i int primary key, quantity varchar(10));
insert into #tab2
select 1, 10 union all
select 2, 20
declare #tab3 table (i int primary key, value varchar(10));
insert into #tab3
select 1, 'value1' union all
select 2, 'value2'
select t1.value,
sum(case when t3.value = 'value1' then t2.quantity else 0 end),
sum(case when t3.value = 'value2' then t2.quantity else 0 end)
from #tab1 t1
join #tab2 t2 on t1.i = t2.i
join #tab3 t3 on t2.i = t3.i
group
by t1.value;

Return non-null value from two tables in Oracle

I have two tables, T1 and T2 with same set of columns. I need to issue a query which will return me value of columns from either table whichever is not null. If both columns are null return null as the value of that column.
The columns are c1,c2,c3,cond1.
I issued the following query. The problem is that if one subquery fails the whole query fails. Somebody please help me. Probably there is another simple way.
SELECT NVL(T1.c1, T2.c1) c1,NVL(T1.c2, T2.c2) c2,NVL(T1.c3, T2.c3) c3
FROM (SELECT c1,c2,c3
FROM T1
WHERE cond1 = 'T10') T1
,(SELECT c1,c2,c3
FROM T2
WHERE cond1 = 'T200') T2 ;
You need something like this:
SELECT NVL((SELECT T1.c1
FROM T1
WHERE T1.c2 = 'T10'),
(SELECT T2.c1
FROM T2
WHERE T2.c2 = 'T200')) AS c1
FROM dual
Or you may prefer a full outer join:
SELECT NVL(T1.c1, T2.c1) AS c1
FROM T1 FULL OUTER JOIN T2 ON 1=1
WHERE T1.c2 = 'T10'
AND T2.c2 = 'T200'
Your result is logical. If the first table is null no combination of values will exist in the natural join.
EDIT. After some new requirements we can use a hack to get the row. Lets get all three possibilities, T1, T2 or all nulls and select the first one:
SELECT *
FROM ( (SELECT T1.*
FROM T1
WHERE T1.c2 = 'T10')
UNION ALL
(SELECT T2.*
FROM T2
WHERE T2.c2 = 'T200')
UNION ALL
(SELECT T2.*
FROM dual
LEFT JOIN T1 ON 1 = 0 ) )
WHERE ROWNUM = 1

left outer join on nullable field with between in join condition (Oracle)

I have two tables as: table1 with fields c1 and dt(nullable); table2 with fields start_dt, end_dt and wk_id. Now I need to perform left outer join between the table1 and table2 to take wk_id such that dt falls between start_dt and end_dt. I applied following condition but some wk_id which shouldn't be NULL are pulled NULL and some rows get repeated.
where nvl(t1.dt,'x') between nvl(t2.start_dt(+), 'x') and nvl(t2.end_dt(+), 'x');
What is wrong with the condition?
select *
from table1 t1
left join table2 t2
on t1.dt between t2.start_dt and t2.end_dt
I recommend you try the new ANSI join syntax.
Also, are you just using 'x' as an example? Or are the dt columns really stored as strings?
It seems you are missing the part "table1 left outer join table2 on table1.some_field = table2.some_field"
Something like this:
select t1.c1, t1.dt, t2.start_dt, t2.end_dt, t2.wk_id
from table1 t1 left outer join table2 t2
on t1.some_field1 = t2.some_field1
where nvl(t1.dt,'x')
between nvl(t2.start_dt, 'x') and
nvl(t2.end_dt, 'x')

Resources