unable to perform conditional insert int hive - hive

I have two tables . Customer1 and Customer2
Customer1
id name
1 jack
2 john
3 jones
Customer 2
id name
The Customer 2 table is empty . Now i have to check if a particular name say 'jack' is present or not in customer 2 and to insert if a name 'jack' is not present in customer 2 .

The below query should server the purpose. I am assuming that ID is the key to link between the tables, if not you can use the name in the join condition.
`insert into customer2
select customer1.*
from customer1
left join customer2
on (customer1.id=customer2.id)
where customer1.name='jack' and isnull(customer2.id);`

Related

Getting records from 2 tables with common an uncommon columns

Below is similar example of the issue I have:
if I have this table 1:
Patient ID
Name
Check in Date
order name
preformed by
1
Jack
12/sep/2002
xray
Dr.Amal
2
Nora
15/oct/2002
ultrasound
Dr.Goerge
1
Jack
13/nov/2003
Medicine
Dr.Fred
table 2:
Patient ID
Name
Check in Date
order name
1
Jack
14/Jun/2002
xray 2
2
Nora
15/oct/2002
ultrasound
1
Jack
13/nov/2003
Medicine
3
Rafael
13/nov/2003
Vaccine
The result I need is as the following:
Name
Check in Date
order name
preformed by
Jack
12/sep/2002
xray
Dr.Amal
Nora
15/oct/2002
ultrasound
Dr.Goerge
Jack
13/nov/2003
Medicine
Dr.Fred
Jack
14/Jun/2002
xray 2
Null
Rafael
13/nov/2003
Vaccine
Null
If you noticed the result I need is all records of table 1 and all records of table 2 with no duplication and joining the same common fields and adding 'Preformed by' column from Table 1. I tried using 'UNION' as the following:
SELECT Name, Check_in_Date, order_name,preformed_by
FROM table1
UNION
SELECT Name, Check_in_Date, order_name,''
FROM table2
the result I get is 2 records for each patient with the same date one with preformed by one with null as the following:
Name
Check in Date
order name
preformed by
Jack
12/sep/2002
xray
Dr.Amal
Nora
15/oct/2002
ultrasound
Dr.Goerge
Nora
15/oct/2002
ultrasound
Null
Jack
13/nov/2003
Medicine
Dr.Fred
Jack
13/nov/2003
Medicine
null
Jack
14/Jun/2002
xray 2
Null
Rafael
13/nov/2003
Vaccine
Null
If the same ID has same check in date in both table it must return the preformed by of table 1 not null How can I do this?
Thank you.
What you need is a FULL JOIN matching by those three columns along with NVL() function in order to bring the values
from table2 which return null from table1 such as
SELECT NVL(t1.name,t2.name) AS name,
NVL(t1.check_in_date,t2.check_in_date) AS check_in_date,
NVL(t1.order_name,t2.order_name) AS order_name,
t1.preformed_by
FROM table1 t1
FULL JOIN table2 t2
ON t1.name = t2.name
AND t1.check_in_date = t2.check_in_date
AND t1.order_name = t2.order_name
or another method uses UNION to filter out duplicates and then applies an OUTER JOIN such as
SELECT tt.name, tt.check_in_date, tt.order_name, t1.preformed_by
FROM (
SELECT name, check_in_date, order_name FROM table1 UNION
SELECT name, check_in_date, order_name FROM table2
) tt
LEFT JOIN table1 t1
ON t1.name = tt.name
AND t1.check_in_date = tt.check_in_date
AND t1.order_name = tt.order_name
Demo

Oracle: Move column to new table, eliminate non-unique rows remaining in original table while maintaining foreign relationship between two tables

I have a table like the following:
Table 1: Person_Favorite_Food
id name address favorite_food
------------------------------------------
1 Dave 123 Cherry Ln Pizza
2 Dave 123 Cherry Ln Cheeseburger
3 Dave 456 Peachtree St Ice cream
4 Cindy 789 Grove Rd Pizza
id - primary key
unique key constraint on the following columns: name, address and food
Since each person can have more than one favorite food item I'd like to split Table 1 into two tables like the following:
Table 2: Person
id name address
--------------------------
1 Dave 123 Cherry Ln
3 Dave 456 Peachtree St
4 Cindy 789 Grove Rd
Table 3: Person_Favorite_Food
person_id favorite_food
-----------------
1 Pizza
1 Cheeseburger
3 Ice cream
4 Pizza
How would I go about doing this in Oracle?
Note: In the original table Rows 1 and 2 represent favorite food for the same person so the favorite food entries in the Person_Favorite_Food table will need to have the same identifier for both of those entries although the identifiers are different in the initial table.
You can use:
create table new1 as
select distinct id, name, address
from t;
create table new2 as
select id, favorite_food
from t;
I would recommend create two new tables and not trying to morph the existing table into one of the new ones.
Use simple aggregation to create table person:
create table person as
select min(id) id, name, address
from person_favorite_food
group by name, address;
Use the same query in merge to change id for some rows:
merge into person_favorite_food a
using (select min(id) id, name, address from person_favorite_food group by name, address) b
on (a.name = b.name and a.address = b.address)
when matched then update set a.id = b.id;
Drop unwanted columns:
alter table person_favorite_food drop (name, address);
Done. Demo in dbfiddle.

find duplicate values in between two tables

I have two tables: Customer1 and Customer2. Both tables' fields are the same, but employee names are different. Some of the employee are repeated in both tables.
I want to find the employees that are duplicated across both tables.
Sample data:
Customer 1
ID Name Designation salary
1 User1 Developer 5000
1 User2 Developer 5000
1 User5 Developer 5000
1 User1 Developer 5000
Customer 2
ID Name Designation salary
1 User1 Developer 5000
1 User2 Developer 5000
1 User3 Developer 5000
1 User1 Developer 5000
Result
ID Name Designation salary
1 User1 Developer 5000
1 User2 Developer 5000
User1 and User2 are in both tables many times, but I want to count them only once. I really appreciate any help on this.
You can join the two tables and the print out the distinct records from Customer1 that are left:
SELECT distinct customer1.*
FROM customer1
INNER JOIN customer2 ON
customer1.id = customer2.id
AND customer1.name = customer2.name
You can use UNION ALL & do aggergation :
select id, name, Designation, salary
from (select c1.id, c1.name, c1.Designation, c1.salary
from Customer1 c1
union all
select c2.id, c2.name, c2.Designation, c2.salary
from Customer2 c2
) c
group by id, name, Designation, salary
having count(*) > 1;

ORACLE SQL Multi Join

I want to join fields from different tables into one doing a query with joins. From now I've only joined two tables but I have difficulties merging others more. Can you help me? These are my tables:
Table Departments
------------------------------------
Department_ID Department_Name
------------------------------------
1 Sales
2 Marketing
3 Warehouse
Table Roles
---------------------------------
Role_ID
---------------------------------
1
2
3
4
Table Departments_Roles
--------------------------------------------------------------------
Dep_Role_ID Department_ID Role_ID Role_Name
--------------------------------------------------------------------
1 1 1 Admin
2 1 2 Client Attention
3 2 1 Admin
4 3 2 Client Attention
Table Employers
---------------------------------
Employer_Id Employer_Name
---------------------------------
1 John
2 Jess
3 Tom
4 George
5 David
What I want to see is:
Table Merged
-------------------------------------------------
Department_Name Employer_Name Role_Name
-------------------------------------------------
xxxxx yyyyy zzzz
This are just some example tables. Dont look for the sense of it.
I've tried using join, but I've never make something so complex.
Can you give me some advice?
Joining multiple tables is the same as joining one table, just repeated.
SELECT D.Department_Name, E.Employer_Name, DR.Role_Name
FROM Employers E --this is your base table
INNER JOIN magicalEmployerToDepartmentRoleLinkTable EDR --this connects our base table to the linking table
ON E.Employer_Id = EDR.Employer_Id
INNER JOIN Department_Roles DR --now we can pull any column in the Department_Roles table that is related back to our base table
ON ED.Department_Id = DR.Department_Id
INNER JOIN Department D --now we can pull any column in the Department table that is related back to the Department_Roles table
ON DR.Department_ID = D.Department_Id

show result from one table

good day, i have these 3 tables...i.e.;
customer table
cust_id cust_name sales_employee
1 abc 1
2 cde 1
3 efg 2
transaction table
order_num cust_id sales_employee
1001 1 1
1002 2 2
sales_employee table
sales_employee employee name
1 john doe
2 jane doe
how can i show the employee name on both customer table and transaction table?
notice how the sales_employee can change per transaction, it does not necessarily have to be the same per customer.
please help.
To select customers with sales person name
select
C.*, E.employee_name
from
Customers as C
inner join Sales_Employees as E on E.sales_employee = C.sales_employee
To select transactions with customer name and salesperson name (at the point in time of the transaction)
select
T.*,
E.employee_name as Trans_employee,
C.cust_name,
EC.employee_name as Cust_employee
from
Transactions as T
inner join Sales_Employees as E on E.sales_employee = T.sales_employee
inner join Customers as C on C.cust_id= T.cust_id
inner join Sales_Employees as EC on EC.sales_employee = C.sales_employee
This code is meant to guide you, you will need to adjust it to match your table and field names.