Select each row and insert into another table - oracle

At a first glance, this is my query
INSERT INTO T2
SELECT T1.*, T3.DATETIME, T3.STATEID
FROM T1 WHERE T3.DATETIME BETWEEN '01-NOV-15' AND SYSDATE
LEFT OUTER JOIN T3
ON (T1.DOCID = T3.DOCID);
T1 AND T2 are almost identicle in terms of number of columns; only T2 has two columns extra (CREATIONDATE, STATEID)
Each and every row of T1 needs to be inserted in T2 + the two additional columns CREATIONDATE & STATEID which are only available in T3. Both T1 and 'T3' share the DOCID which could be used to join them...
Some issues remain, help is much appreciated..
Getting Error
Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"

The problem in in the position of the WHERE clause, which has to come after the list of joined table:
INSERT INTO T2
SELECT T1.*, T3.DATETIME, T3.STATEID
FROM T1
LEFT OUTER JOIN T3
ON (T1.DOCID = T3.DOCID)
WHERE T3.DATETIME BETWEEN '01-NOV-15' AND SYSDATE;

Your syntax is incorrect, where comes after the join:
INSERT INTO T2
SELECT T1.*, T3.DATETIME, T3.STATEID
FROM T1
LEFT OUTER JOIN T3
ON (T1.DOCID = T3.DOCID)
WHERE T3.DATETIME BETWEEN '01-NOV-15' AND SYSDATE

Related

getting error- ORA-00905: missing keyword

Select * from table1 t1 left outer join table2 t2 on t1.id=t2.id and
case
when t1.id in (select t2.id from table2)
then t1.valid_to_ts > sysdate and t2.valid_to_ts>sysdate
else
t1.valid_to_ts>sysdate.
getting error-
ORA-00905: missing keyword
You can use UNIONs to implement the logic of the case:
SELECT *
FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE t2.id IS NULL
AND t1.valid_to_ts > SYSDATE
UNION ALL
SELECT *
FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE t1.id IS NOT NULL
AND t1.valid_to_ts > SYSDATE
AND t2.valid_to_ts > SYSDATE;
As Álvaro González said in a comment, you can't use a case expression as a flow control operator. You can use a case expression in a where or on clause, but only by making it generate a value which you then compare with something else, which can be awkward, so it's usually better not to. You can usually replace the logic you're trying to implement with simple Boolean logic.
In this case you're already joining to table2 so your subquery isn't needed; you can use a where clause to see if a matching record was found - preferably checking for a not-null column other than ID, so this example will only work if valid_to_ts is not nullable:
select * from table1 t1
left outer join table2 t2
on t1.id = t2.id
where t1.valid_to_ts > sysdate
and (t2.valid_to_ts is null or t2.valid_to_ts > sysdate)
If t2.valid_to_ts is nullable then you should use a different not-nullable column instead; unless you want to include values with no valid-to date - but you aren't doing that for t1.valid_to_ts.
If you try to check that in the on clause then you won't filter out IDs which do exist in table2 but with an earlier date.
db<>fiddle, including an on-clause version which gets the wrong result (as far as I can tell from your starting query anyway).
For the demo I've assumed that the _ts columns are dates, since you're comparing with sysdate; if they are actually timestamps (as the suffix might suggest) then you could compare with systimestamp instead.

Workaround for delete in hive with join conditions

So i'm trying to convert a SQL delete query to Hive one. Im using hive .12 version's which doesn't supports delete.
Below is the SQL query:
Delete from t1 c where exists(select 1 from t2 a where
a.emplid=c.employee_id and a.project_status='test')
Now i tried tried using NOT IN for the above query but due to some reasons we cannot use NOT IN in our queries.
below is Hive query i have written but i'm not sure as its not giving correct results. i'm pretty new to hive. can anyone help on this.
INSERT Overwrite table t1
select * from t1 c left outer join t2 a on (c.employee_id=a.employee_id)
where a.project_status= 'test'
and a.employee_id is null
Move project_status='test' condition to the subquery or into the on clause. Also you should select columns only from table c.
Example with filter in the subquery:
insert overwrite table t1
select c.*
from t1 c
left join (select employee_id
from t2
where project_status='test'
) a on (c.employee_id=a.employee_id)
where a.employee_id is null;
Example with additional condition in the ON:
insert overwrite table t1
select c.*
from t1 c
left join t2 a on (c.employee_id=a.employee_id and a.project_status='test')
where a.employee_id is null;

USING multiple 'OR' conditions in JOIN component in Oracle data Integrator 12c

I want to use multiple 'OR' conditions in JOIN component in Oracle data Integrator 12c.
Conditions to be taken care when doing the above task is:
Say table T1 and T2, I need to take left outer join on T1(i.e. I need all the records from T1 for multiple satisfied join conditions specified in JOIN component in ODI 12c)
For example:
a. For table T1, T2: say conditions c1, c2, c3. T1 Left outer join T2.
b. I want to get the data in table say T3: Ensuring all records from T1 PLUS all records from T2 for all the conditions satisfied(namely c1,c2,c3).
Sample query:
select T1.*
from T1 LEFT OUTER JOIN T2
ON (C1 OR C2 OR C3);
Kindly help me on this at the earliest.
Thanks in advance!
You can try either query both will get you all the rows from T1 that either matched with T2 columns respectively or didn't have any match with T2.
Using UNION
SELECT DISTINCT *
FROM (
SELECT T1.*
FROM T1
LEFT OUTER JOIN T2 ON T1.day = T2.day
UNION
SELECT T1.*
FROM T1
LEFT OUTER JOIN T2 ON T1.month = T2.month
UNION
SELECT T1.*
FROM T1
LEFT OUTER JOIN T2 ON T1.yearly = T2.yearly
) as T3;
Using OR (NOTE: displaying T2 columns just to show that LEFT JOIN is working on each condition)
SELECT T1.*, T2.*
FROM T1
LEFT OUTER JOIN T2 ON
(T1.day = T2.day OR T1.month = T2.month OR T1.yearly = T2.yearly)
Sample Run
I have 4 records in T1 and 3 records in T2. Records in T1 are such that 3 rows
match with exactly 1 column in T2 and 4th row doesnt match any records in T2.
Output of both the queries gets what you need.

Hive :Insert the records that are not present

I need to insert records into a table t1 from another table t2 such that insert only the records that are not in t2.
But when i use this query
insert into table t1 select * from t2 where id not in (select id from t1);
But I get error as
Correlating expression cannot contain qualified column reference.
Can anybody suggest me a query to do this.
t2.id
Yet another ridiculous hive limitation
insert into table t1 select * from t2 where t2.id not in (select id from t1);
You can also use below command :-
insert into table t1 select t2.* from t2 left join t1 on t2.id=t1.id where t1.id is NULL;

I want to update T1 using T3 column and by using three table relationships

UPDATE TABLE1 T1 SET T1.CENTERNAME=
(SELECT AC.CENTERNAME
FROM TABLE2 T2 INNER JOIN TABLET3 AN ON T2.CENTERID = T3.LOCATIONID
INNER JOIN TABLE1 T1 ON T3.LOG_ID = T1.LOGID W
HERE TRUNC(T1.ROW_DATE)='25-MAR-2014');
This gives the error 'ORA-01427: single-row subquery returns more than one row'.
The error message
ORA-01427: single-row subquery returns more than one row
means, er, the sub-query returns more than row. That is, this part of your statement ...
(SELECT AC.CENTERNAME
FROM TABLE2 T2 INNER JOIN TABLET3 AN ON T2.CENTERID = T3.LOCATIONID
INNER JOIN TABLE1 T1 ON T3.LOG_ID = T1.LOGID
WHERE TRUNC(T1.ROW_DATE)='25-MAR-2014')
returns more than row. The error occurs because the SET part of the UPDATE depends on the equality operator - SET T1.CENTERNAME= - so it can take only be one value.
Without more details about your data structure it is hard to be certain but I suspect what you really want is something like this
UPDATE TABLE1 T1
SET T1.CENTERNAME= (SELECT T2.CENTERNAME
FROM TABLE2 T2
INNER JOIN TABLE3 T3
ON T2.CENTERID = T3.LOCATIONID
WHERE T3.LOG_ID = T1.LOGID )
WHERE TRUNC(T1.ROW_DATE)='25-MAR-2014'
/
(I've tidied up your redaction to make the aliases consistent.)

Resources