Column mismatch error while inserting data from one table to another - hive

I'm trying to insert data from one table to another, jud_pers_record_leg_proced_update_202208 is replica of eden_es_master_db.jud_pers_record_leg_proced but when I'm using select distinct in insert query, it consider that eden_es_master_db.jud_pers_record_leg_proced only has those distinct column,
INSERT INTO eden_es_master_db.jud_pers_record_leg_proced_update_202208 partition(edenloaddate)
select distinct edenloaddate, newidentifierlegalproceeding
from eden_es_master_db.jud_pers_record_leg_proced
where personidentifiercao IN (select cao from eden_es_dynamic.fisbajas where cao is not null);
below is the error message
Error: Error while compiling statement: FAILED: SemanticException [Error 10044]: Line 1:12 Cannot insert into target table because column number/types are different 'edenloaddate': Table insclause-0 has 21 columns, but query has 2 columns. (state=42000,code=10044)

To load into a partitioned table, you need to ensure your last column always be partition column so change your SQL like this -
INSERT INTO eden_es_master_db.jud_pers_record_leg_proced_update_202208 partition(edenloaddate)
select distinct edenloaddate, newidentifierlegalproceeding , edenloaddate -- last partition column
from eden_es_master_db.jud_pers_record_leg_proced
where personidentifiercao IN (select cao from eden_es_dynamic.fisbajas where cao is not null);

Related

Why am I getting the error: "Ambiguous column" in my query?

In this query I inserting records into a new empty table I created. These records are derived from another table where I am left joining that table to itself, in order to output records that are not included in the recent table that is appended on top of an older table. So basically it outputs records that were deleted.
CREATE DEFINER=`definer` PROCEDURE `stored_procedure_name`()
MODIFIES SQL DATA
SQL SECURITY INVOKER
BEGIN
START TRANSACTION;
INSERT INTO exceptions_table (
`insert_date`,
`updated`,
`account_number`,
`id_number`)
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
FROM original_table ot1
LEFT JOIN original_table ot2
ON ot1.`account_number` = vdcaas2.`account_number`
AND ot2.`insert_date` = '2022-12-20'
WHERE ot1.`insert_date` = '2022-12-10'
AND ot2.`account_number` IS NULL;
COMMIT;
END
I get an error stating: "SQL Error: Column "insert_date" in field list is ambiguous.
I'm not sure why because I have specified which table I am grabbing "insert_date" from when INSERTING and when SELECTING and JOINING..
Every row in your query has two columns called insert_date: one from the table you've aliased as "ot1", and one from the table (as it happens, the same table) you've aliased as "ot2".
The database system doesn't know which one you want, so you have to tell it by writing either "ot1.insert_date" or "ot2.insert_date", just as you do elsewhere in the query:
... ot2.`insert_date` = '2022-12-20'
...
... ot1.`insert_date` = '2022-12-10'
The same is true of the other columns you've listed to select.
You need to change this
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
to this
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
or this
SELECT
ot2.`insert_date`,
ot2.`updated`,
ot2.`account_number`,
ot2.`id_number`
or some combination
Issue
SQL Error: Column "insert_date" in field list is ambiguous error means that the query is trying to reference the "insert_date" column from both tables, ot1 and ot2.
Try the following:
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
Also, you have a typo in your query:
ON ot1.`account_number` = vdcaas2.`account_number` -> ON ot1.`account_number` = ot2.`account_number`

insert into two different tables from two different tables

When i try this, i got error
"ORA-06550: line 2, column 48: PL/SQL: ORA-00926: missing VALUES
keyword ORA-06550: line 2, column 1: PL/SQL: SQL Statement ignored".
I need to insert some values from different tables to two different tables.
assume table 1 and table 2 are like ,
table 1
x|y ---> column names
a b ---> values
table 2
z|k --->c.n
c d --->val.
As you see i need to copy some rows with little changes to its own table.But i have two different tables and I should do that at the same time.
INSERT INTO table1.a,table1.b,table2.c,table2.d
SELECT x,y,z,k
FROM table1
FULL JOIN table2_ALT ON table1.x=table2.z
WHERE ....
or
INSERT INTO table1.a,table1.b,table2.c,table2.d
SELECT table1.x,table1.y,table2.z,table2.k
FROM table1
FULL JOIN table2_ALT ON table1.x=table2.z
WHERE ....
You can try something similar like given below: Read more about INSERT ALL for inserting into multiple tables.
INSERT ALL
INTO
table1(a,b) values(a, b)
INTO table2(c,d) values(c, d)
SELECT table1.x as a,table1.y as b,table2.z as c,table2.k as d
FROM table1
FULL JOIN table2_ALT ON table1.x=table2.z
WHERE ....

Insert column from one table to another

I want to copy a column from one table to another.
The number of rows is equal in both tables. The values I want to copy from table2 to table1 are unique. I've tried a few thing, but none so far work. My code is:
insert into alleoppdragpunkter3
select Idtall
from IDtall
Msg 2809, Level 16, State 1, Line 2
The request for procedure 'IDtall' failed because 'IDtall' is a table object.
I would like my column from table2 to be in table1.
You can try below-
insert into alleoppdragpunkter3(col1,col2,col3,....)
select col1,col2,col3,.... from IDtall
You do not copy columns between tables. You can insert rows and update columns.
Perhaps you want:
update p
set p.<col> = i.<col>
from alleoppdragpunkter3 p join
idtall i
on p.? = i.?;
The ? is for the column that specify the join conditions between the tables. The set references the column you want to update and which value to take.

Delete a record in Hive based on a table records in another database

I have two hive databases namely db1 and db2. I have a table in db1 called table1, and a table in db2 called table2. I want to delete some rows in table2 based on particular column values in table1.
I use the below query but it fails
DELETE FROM db2.table2
WHERE db2.table2.F_SESSION IN (
SELECT F_SESSION FROM db1.table1
WHERE db1.table1.STATUS = 1);
The error is
Error while compiling statement: FAILED: SemanticException [Error 10004]: Line 4:12 Invalid table alias or column reference 'db1': (possible column names are: f_session, status)
Any clue on where I am going wrong? Btw, I am not an experienced SQL person.
Try this,
DELETE FROM db2..table2
WHERE db2..table2.F_SESSION IN (
SELECT F_SESSION FROM db1..table1
WHERE db1..table1.STATUS = 1);
But this worked
USE db2;
DELETE FROM table2 WHERE table2.F_SESSION IN (
SELECT F_SESSION FROM db1.table1 AS T1
WHERE T1.STATUS = 1);

SQL Query to return rows even if it is not present in the table

This is a specific problem .
I have an excel sheet containing data. Similar data is present in a relational database table. Some rows may be absent or some additional rows may be present. The goal is to verify the data in the excel sheet with the data in the table.
I have the following query
Select e_no, start_dt,end_dt
From MY_TABLE
Where e_no In
(20231, 457)
In this case, e_no 457 is not present in the database (and hence not returned). But I want my query to return a row even if it not present (457 , null , null). How do I do that ?
For Sql-Server: Use a temporary table or table type variable and left join MY_TABLE with it
Sql-Server fiddle demo
Declare #Temp Table (e_no int)
Insert into #Temp
Values (20231), (457)
Select t.e_no, m.start_dt, m.end_dt
From #temp t left join MY_TABLE m on t.e_no = m.e_no
If your passing values are a csv list, then use a split function to get the values inserted to #Temp.
Why not simply populate a temporary table in the database from your spreadsheet and join against that? Any other solution is probably going to be both more work and more difficult to maintain.
You can also do it this way with a UNION
Select
e_no, start_dt ,end_dt
From MY_TABLE
Where e_no In (20231, 457)
UNION
Select 457, null, null