Hive query: Join tables based on ID

0 votes

Hello,
I have 2 tables as below with the data loaded as below, I need a query to join 2 tables on id whenever the charge is -ve values, there should not be any duplicate values 
ex:id 20 has 2 transactions each with charge -1:00 since the charge is -ve we should not see duplicate values in the joined query for id 20.
Please help me with the query

create table a1
(
id int,
name string
)
row format delimited fields terminated by ","
stored as textfile

create table b1
(
id int,
charge float
)
row format delimited fields terminated by ","
stored as textfile


INSERT INTO a1 values 
(10, "sant"),
(20, "suvi"),
(25, "kal"),
(26, "deep"),
(27, "satya"),
(30, "manju")

INSERT INTO b1 values 
(10, 1.00),
(20, -1.00),
(25, 1.00),
(26, -1.00),
(27, 1.00),
(28, -1.00),
(20, -1.00),
(26, -1.00),
(28, -1.00),
(10, 2.00)
Jul 25 in Big Data Hadoop by Ramya
16 views

1 answer to this question.

0 votes

After creating the tables a1 and b1 and loading data in it, follow the below steps,

1) We will create a new table as below,

create table b2
(
id int,
charge float
)
stored as textfile;

2) Load data in this table,

insert into table b2 select id, charge from b1 group by id, charge having charge > 0;

3) Display the desired result,

select a.name, b.id, b.charge from a1 a join b2 b on (a.id = b.id);
answered Jul 25 by Tarun

Related Questions In Big Data Hadoop

0 votes
1 answer

Query regarding Temporary table's metadata in HIVE

Registered tables are not cached in memory. ...READ MORE

answered May 22 in Big Data Hadoop by Tina
29 views
0 votes
1 answer

How Impala is fast compared to Hive in terms of query response?

Impala provides faster response as it uses MPP(massively ...READ MORE

answered Mar 21, 2018 in Big Data Hadoop by nitinrawat895
• 10,670 points
322 views
0 votes
1 answer

Setting Hive/Hadoop property using Hive Query

You can set Hadoop & Hive conf ...READ MORE

answered Apr 18, 2018 in Big Data Hadoop by Shubham
• 13,300 points
258 views
0 votes
1 answer

What is the difference between Hive internal tables and external tables?

Hive has a relational database on the ...READ MORE

answered Nov 19, 2018 in Big Data Hadoop by Neha
• 6,280 points
698 views
0 votes
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,670 points
2,998 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,670 points
334 views
0 votes
10 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyFr ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
14,797 views
0 votes
1 answer

Hadoop dfs -ls command?

In your case there is no difference ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by kurt_cobain
• 9,240 points
1,101 views
0 votes
1 answer

Hive: How to use insert query like SQL

It is now possible to insert like ...READ MORE

answered Nov 5, 2018 in Big Data Hadoop by Omkar
• 67,620 points
63 views
–1 vote
1 answer

Beeline and Hive Query Editor in Embedded mode

Running Hive client tools with embedded servers ...READ MORE

answered Dec 31, 2018 in Big Data Hadoop by Omkar
• 67,620 points
244 views