how to use two column in different table and use between operator. in hive - hadoop

I have two table say A and B in Hive. table A has "position" column and table B has "startposition" and "endposition" column. I am trying to do something like .
select * from A where position between (select startposition from B) AND (select endposition from B);
But it is not working and given exception .
how could we do that so each position of table A is check against each pair of startposition and endposition of table B.

select
position
from A
join B
where
A.position > B.startposition AND A.position<B.endposition;
LanguageManual Joins

Related

Get Remaining row from both common tables in Oracle

I have two tables which has data in it. Below are the details.
Table A:- tbl_ipcolo_mast_info -> 42K records
Table B:- tbl_ipcolo_mast_info_dump-> 40K records
Table A consist of all the table B. I want only the other 2k records from table A. How to get it.
Try this:
select * from tbl_ipcolo_mast_info
where <PK_COLUMN> in
(select <PK_COLUMN> from
tbl_ipcolo_mast_info
minus
select <PK_COLUMN> from
tbl_ipcolo_mast_info_dump)
-- update --
According to our comments below, table A and table B both have identical columns, the simple MINUS operator can be used to achieve the desired result:
select * from tbl_ipcolo_mast_info
MINUS
select * from tbl_ipcolo_mast_info_dump
To check if both the tables have an identical structure, please query on the dictionary view: USER_TAB_COLS
Cheers!!
You can use MINUS for that:
select *
from tbl_ipcolo_mast_info
minus
select *
from tbl_ipcolo_mast_info_dump;
The above assumes that the structure of both tables is identical. If they are different you need to explicitly list the matching columns in each SELECT part.
select column list from tbl_ipcolo_mast_info
minus
select column list from tbl_ipcolo_mast_info_dump
Since the 2 tables do not have the same columns use NOT EXISTS:
select * from tbl_ipcolo_mast_info t
where not exists (
select 1 from tbl_ipcolo_mast_info_dump d
where <conditions here>
)
Replace <conditions here> with conditions like:
d.columnd1 = t.columnt1 and d.columnd2 = t.columnt2 and ....
Or with a LEFT JOIN:
select t.*
from tbl_ipcolo_mast_info t left join tbl_ipcolo_mast_info_dump d
on d.columnd1 = t.columnt1 and d.columnd2 = t.columnt2 and ....
where d.columnd1 is null

Hive Joins query

I have two tables in hive:
Table 1:
1,Nail,maher,24,6.2
2,finn,egan,23,5.9
3,Hadm,Sha,28,6.0
4,bob,hope,55,7.2
Table 2 :
1,Nail,maher,24,6.2
2,finn,egan,23,5.9
3,Hadm,Sha,28,6.0
4,bob,hope,55,7.2
5,john,hill,22,5.5
6,todger,hommy,11,2.2
7,jim,cnt,99,9.9
8,will,hats,43,11.2
Is there any way in Hive to retrieve the new data in table 2 that doesn't exist in table 1??
In other Databases tools, you would use a inner left/right. But inner left/right doesn't exist in Hive and suggestions how this could be achieved?
If you are using Hive version >= 0.13 you can use this query:
SELECT * FROM A WHERE A.firstname, A.lastname ... IN (SELECT B.firstname, B.lastname ... FROM B);
But I'm not sure if Hive supports multiple coloumns in the IN clause.
If not something like this could work:
SELECT * FROM A WHERE A.firstname IN (SELECT B.firstname FROM B) AND A.lastname IN (SELECT b.lastname FROM B) ...;
It might be wiser to concatenate the fields together before testing for NOT IN:
SELECT *
FROM t2
WHERE CONCAT(t2.firstname, t2.lastname, CAST(t2.val1 as STRING), CAST(t2.val2 as STRING)) NOT IN
(SELECT CONCAT(t2.firstname, t2.lastname, CAST(t2.val1 as STRING), CAST(t2.val2 as STRING))
FROM t1)
Performing sequential NOT IN sub-queries may give you erroneous results.
From the above example, a new record with the values ('nail','egan',28, 7.2) would not show up as new with sequential NOT IN statements.

joining two tables with one table in oracle

I have a table pa_master_details and lov_details.
In the pa_master_details table, I have role_comp_emp_final_rating and role_comp_lm_final_rating columns.
In the lov_details table I have a column rating lov_value and lov_text_en.
I need to join role_comp_emp_final_rating with the lov_value column to get lov_text_en
Based on this lov_value, I need to show lov_text_en from the lov_details table.
So I wrote a query like this and am getting the result for emp rating:
SELECT p.employee_number,
p.role_comp_emp_final_rating,
lov_text_en,
p.role_comp_lm_final_rating
FROM pa_master_details P, lov_details L
WHERE p.role_comp_emp_final_rating = l.lov_value
AND p.employee_number = 34570
Similarly, I need to show lov_text_en for role_comp_lm_final_rating from the lov_details table by joining
role_comp_lm_final_rating with lov_value in the same query.
How do I do it?
I think this is what you're asking for:
SELECT p.employee_number,
p.role_comp_emp_final_rating,
lemp.lov_text_en AS "emp_lov_text",
llm.lov_text_en AS "lm_lov_text",
p.role_comp_lm_final_rating
FROM pa_master_details p
JOIN (SELECT lov_text FROM lov_details) lemp ON p.role_comp_emp_final_rating = lemp.lov_value
JOIN (SELECT lov_text FROM lov_details) llm ON p.role_comp_lm_final_rating = llm.lov_value
WHERE p.employee_number = 34570

Assignment in Hive query

I have below query in which i need to assign one table column value to another table column.
Query:
SELECT A.aval,B.bval,B.bval1 FROM A JOIN B ON (A.aval = B.bval)
How do I assign one table column value to another table column in Hive?
Have tried
SELECT A.aval,B.bval,B.bval1, A.aval = B.bval1 FROM A JOIN B ON (A.aval = B.bval)
In results:
A.aval = B.bval1, returning false since its not assigning to A.aval.
I guess you want to write in a table ?
So You have to create a table (for example C) which contains all the fields you need.
And then you do :
INSERT [OVERWRITE] INTO TABLE C
SELECT A.aval,B.bval,B.bval1, A.aval
FROM A
JOIN B ON (A.aval = B.bval)
The result of the select will be inserted in the table C
insert overwrite table c SELECT A.aval,B.bval,B.bval1 FROM A JOIN B ON (A.aval = B.bval)

Oracle, insert multirows from subquery with more than one row

Im trying to copy some field from a table to other, I want to do iy by using insert with a subquery like this:
insert into sed_reporte_generico
(srg_usuario,
srg_nombres,
srg_ape_paterno,
srg_ape_materno,
srg_objetivo,
srg_peso_ob,
srg_calf_ob)
values
(
(select us.su_st_usuario, us.su_st_nombres, us.su_st_ap_paterno, us.su_st_ap_materno, ob.soc_st_descripcion, ob.soc_nr_peso,ob.soc_nr_calificacion
from sed_objetivo ob, sed_usuarios us, sed_evaluacion ev
where ob.se_evaluacion_pk = ev.se_evaluacion_pk and ev.su_colaborador_fk = us.su_usuarios_pk)
);
but I got this error:
01427. 00000 - "single-row subquery returns more than one row"
any idea how should I do this?
Thanks,
Think you got to choose between
insert into table (a, b, c) VALUES(1, 2, 3)
and
insert into table (a, b, c)
(SELECT x, y, z from table2)
You can have VALUES and SELECT mixed only (as pointed by an anomyous horse)
when your select query returns only one column and one row !
By the way, use JOIN... to Join your tables (use of the WHERE clauses to join tables is rather a bad habit) :
INSERT INTO sed_reporte_generico
(srg_usuario,
srg_nombres,
srg_ape_paterno,
srg_ape_materno,
srg_objetivo,
srg_peso_ob,
srg_calf_ob)
(select us.su_st_usuario, us.su_st_nombres, us.su_st_ap_paterno, us.su_st_ap_materno, ob.soc_st_descripcion, ob.soc_nr_peso,ob.soc_nr_calificacion
FROM sed_objetivo ob
JOIN sed_evaluacion ev ON ob.se_evaluacion_pk = ev.se_evaluacion_pk
JOIN sed_usuarios us on ev.su_colaborador_fk = us.su_usuarios_pk)
);

Resources