1

I have a table from following command:

CREATE TABLE treatment_costs AS SELECT * FROM 
(SELECT r.patient_id, r.transaction_date, r.paid_transaction_amount, o.dob, o.department_name, o.reason_of_visit FROM ReceiptTransactions r
LEFT OUTER JOIN OpdPatientQ o ON (r.patient_id = o.patient_id)
);

I now want to insert all records that are inserted today(on the given day) into the above table. To do this, I have written:

INSERT INTO TABLE treatment_costs SELECT * FROM
(SELECT r.patient_id, r.transaction_date, r.paid_transaction_amount, o.dob, o.department_name, o.reason_of_visit FROM ReceiptTransactions r WHERE timestamp_column = today_date
LEFT OUTER JOIN OpdPatientQ o ON (r.patient_id = o.patient_id)
);

is this the correct way to insert multiple queries into a table?

EDIT 1: For example my table treatment_costs's contents are these rows:

patient_id, transaction_date, paid_transaction_amount, dob, department_name, reason_of_visit
001 01/01/2014 30000 01/01/1985 Cardiology reason_1
002 01/01/2014 35000 01/01/1975 Cardiology reason_2
003 02/01/2014 40000 01/01/1965 Oncology   reason_3
004 02/01/2014 30000 01/01/1985 Cardiology reason_4
005 02/01/2014 20000 01/01/1975 Gynecology reason_5

and my doubt now is select statement in my insert query, which is:

SELECT * FROM
(SELECT r.patient_id, r.transaction_date, r.paid_transaction_amount, o.dob, o.department_name, o.reason_of_visit FROM ReceiptTransactions r WHERE timestamp_column = today_date
LEFT OUTER JOIN OpdPatientQ o ON (r.patient_id = o.patient_id)
);

, For example, gives below result:

patient_id, transaction_date, paid_transaction_amount, dob, department_name, reason_of_visit
011 01/01/2015 30000 01/01/1986 Cardiology reason_11
012 01/01/2015 35000 01/01/1976 Cardiology reason_21
013 02/01/2015 40000 01/01/1966 Oncology   reason_31
014 02/01/2015 30000 01/01/1986 Cardiology reason_41
015 02/01/2015 20000 01/01/1976 Gynecology reason_51

And, would the contents of my table after executing insert query be like this below?

patient_id, transaction_date, paid_transaction_amount, dob, department_name, reason_of_visit
001 01/01/2014 30000 01/01/1985 Cardiology reason_1
002 01/01/2014 35000 01/01/1975 Cardiology reason_2
003 02/01/2014 40000 01/01/1965 Oncology   reason_3
004 02/01/2014 30000 01/01/1985 Cardiology reason_4
005 02/01/2014 20000 01/01/1975 Gynecology reason_5
011 01/01/2015 30000 01/01/1986 Cardiology reason_11
012 01/01/2015 35000 01/01/1976 Cardiology reason_21
013 02/01/2015 40000 01/01/1966 Oncology   reason_31
014 02/01/2015 30000 01/01/1986 Cardiology reason_41
015 02/01/2015 20000 01/01/1976 Gynecology reason_51
Naveen Reddy Marthala
  • 2,622
  • 4
  • 35
  • 67

1 Answers1

2

Excerpt from the Hive Language Manual,

INSERT INTO will append to the table or partition, keeping the existing data intact.

INSERT INTO TABLE ...

will not overwrite any data that is already present in the table. The INSERT query you have will run a MapReduce (based on the engine type) job which will write the newly generated files into the table location without deleting the existing ones.

franklinsijo
  • 17,784
  • 4
  • 45
  • 63