Oracle, insert multirows from subquery with more than one row - oracle

Im trying to copy some field from a table to other, I want to do iy by using insert with a subquery like this:
insert into sed_reporte_generico
(srg_usuario,
srg_nombres,
srg_ape_paterno,
srg_ape_materno,
srg_objetivo,
srg_peso_ob,
srg_calf_ob)
values
(
(select us.su_st_usuario, us.su_st_nombres, us.su_st_ap_paterno, us.su_st_ap_materno, ob.soc_st_descripcion, ob.soc_nr_peso,ob.soc_nr_calificacion
from sed_objetivo ob, sed_usuarios us, sed_evaluacion ev
where ob.se_evaluacion_pk = ev.se_evaluacion_pk and ev.su_colaborador_fk = us.su_usuarios_pk)
);
but I got this error:
01427. 00000 - "single-row subquery returns more than one row"
any idea how should I do this?
Thanks,

Think you got to choose between
insert into table (a, b, c) VALUES(1, 2, 3)
and
insert into table (a, b, c)
(SELECT x, y, z from table2)
You can have VALUES and SELECT mixed only (as pointed by an anomyous horse)
when your select query returns only one column and one row !
By the way, use JOIN... to Join your tables (use of the WHERE clauses to join tables is rather a bad habit) :
INSERT INTO sed_reporte_generico
(srg_usuario,
srg_nombres,
srg_ape_paterno,
srg_ape_materno,
srg_objetivo,
srg_peso_ob,
srg_calf_ob)
(select us.su_st_usuario, us.su_st_nombres, us.su_st_ap_paterno, us.su_st_ap_materno, ob.soc_st_descripcion, ob.soc_nr_peso,ob.soc_nr_calificacion
FROM sed_objetivo ob
JOIN sed_evaluacion ev ON ob.se_evaluacion_pk = ev.se_evaluacion_pk
JOIN sed_usuarios us on ev.su_colaborador_fk = us.su_usuarios_pk)
);

Related

How can I join, aggregate, and pivot data that includes a BLOB column?

I'm trying to aggregate and pivot data from one table while joining two others. The third table contains a BLOB (which isn't allowed and causes ORA-00932: inconsistent datatypes: expected - got BLOB)
What I would like to run (but cannot):
SELECT * FROM (
select p.blah, p.result, p.type, s.blah, b.blob
from table p
join table2 s on s.id = p.id
join tableBlob b on b.id = s.id)
PIVOT (max(result) FOR (type) IN ('type1' as type1, 'type2' as type2))
where blah = 'value'
ORDER BY blah;
How might I achieve the desired results?

Insert Statement Returns ORA-01427 Error While Trying To Insert From Multiple Tables

I have this table F_Flight which I am trying to insert into from 3 different tables. The first, fourth and fifth columns are from the same, and the second and third columns from different tables. When I execute the code, I get a "single-row subquery returns more than one row" error.
insert when 1 = 1 then into F_Flight (planeid, groupid, dateid, flightduration, kmsflown) values
(planeid, (select b.groupid from BridgeTable b where exists (select p.p1id from pilotkeylookup p where b.pilotid = p.p1id)),
(select dd.id from D_Date dd where exists (select p.launchtime from PilotKeyLookup p where dd."Date" = p.launchtime)),
flightduration, kmsflown) select * from PilotKeyLookup p;
Your subqueries get multiple rows back, which is what the error message says. There is no correlation between the various bits of data and subqueries you're trying to insert into a single row.
This can be done as a much simpler insert...select with joins, something like:
insert into f_flight (planeid, groupid, dateid, flightduration, kmsflown)
select pkl.planeid, bt.groupid, dd.id, pkl.flightduration, pkl.kmsflown
from pilotkeylookup pkl
join bridgetable bt on bt.pilotid = pkl.p1id
join d_date dd on dd."Date" = pkl.launchtime;
This joins the main PilotKeyLookup table to the other two on the keys you used in your subqueries.
Storing an ID value instead of an actual date is unusual, and if launchtime has a time component - which seems likely from the name - and your d_date entries are just dates (i.e. all with time at midnight) then you won't find matches; you might need to do:
join d_date dd on dd."Date" = trunc(pkl.launchtime);
It also seems like this could be a view, as you're storing duplicate data - everything in f_flight could, obviously, be found from the other tables.

how to use two column in different table and use between operator. in hive

I have two table say A and B in Hive. table A has "position" column and table B has "startposition" and "endposition" column. I am trying to do something like .
select * from A where position between (select startposition from B) AND (select endposition from B);
But it is not working and given exception .
how could we do that so each position of table A is check against each pair of startposition and endposition of table B.
select
position
from A
join B
where
A.position > B.startposition AND A.position<B.endposition;
LanguageManual Joins

joining two tables with one table in oracle

I have a table pa_master_details and lov_details.
In the pa_master_details table, I have role_comp_emp_final_rating and role_comp_lm_final_rating columns.
In the lov_details table I have a column rating lov_value and lov_text_en.
I need to join role_comp_emp_final_rating with the lov_value column to get lov_text_en
Based on this lov_value, I need to show lov_text_en from the lov_details table.
So I wrote a query like this and am getting the result for emp rating:
SELECT p.employee_number,
p.role_comp_emp_final_rating,
lov_text_en,
p.role_comp_lm_final_rating
FROM pa_master_details P, lov_details L
WHERE p.role_comp_emp_final_rating = l.lov_value
AND p.employee_number = 34570
Similarly, I need to show lov_text_en for role_comp_lm_final_rating from the lov_details table by joining
role_comp_lm_final_rating with lov_value in the same query.
How do I do it?
I think this is what you're asking for:
SELECT p.employee_number,
p.role_comp_emp_final_rating,
lemp.lov_text_en AS "emp_lov_text",
llm.lov_text_en AS "lm_lov_text",
p.role_comp_lm_final_rating
FROM pa_master_details p
JOIN (SELECT lov_text FROM lov_details) lemp ON p.role_comp_emp_final_rating = lemp.lov_value
JOIN (SELECT lov_text FROM lov_details) llm ON p.role_comp_lm_final_rating = llm.lov_value
WHERE p.employee_number = 34570

Comparing two tables, if rows are different, run query in Oracle

Think of my two tables have the same columns. One column is the ID, and the other one is the text. Is it possible to implement the following pseudo code in PLSQL?
Compare each row (They will have the same ID)
If anything is different about them
Run a couple of queries: an Update, and an Insert
ElseIf they are the same
Do nothing
Else the row does not exist
So add the row to the table compared on
Is it easy to do this using PLSQL or should I create a standalone application to do do this logic.
As your table have the same columns, by using NATURAL JOIN you can easily check if two corresponding rows are identical -- without need to update your code if a column is added to your table.
In addition, using OUTER JOIN allow you to find the rows present in one table but not in the other.
So, you can use something like that to achieve your purpose:
for rec in (
SELECT T.ID ID1,
U.ID ID2,
V.EQ
FROM T
FULL OUTER JOIN U ON T.ID = U.ID
FULL OUTER JOIN (SELECT ID, 1 EQ FROM T NATURAL JOIN U) V ON U.ID = V.ID)
loop
if rec.id1 is null
then
-- row in U but not in T
elsif rec.id2 is null
then
-- row in T but not in U
elsif rec.eq is null
-- row present in both tables
-- but content mismatch
end if
end loop
Else the row does not exist
So add the row to the table compared on
Is this condition means that rows can be missed in both tables? If only in one, then:
insert into t1 (id, text)
select id, text
from t2
minus
select id, text
from t1;
If missed records can be in both tables, you need the same query that inserts into table t2 rows from t1.
If anything is different about them
If you need one action for any amount of different rows, then use something like this:
select count(*)
into a
from t1, t2
where t1.id = t2.id and t1.text <> t2.text;
if a > 0 then
...
otherwise:
for i in (
select *
from t1, t2
where t1.id = t2.id and t1.text <> t2.text) loop
<do something>
end loop;
A 'merge' statement is what u needed.
Here is the syntax:
MERGE INTO TARGET_TABLE
USING SOURCE_TABLE
ON (CONDITION)
WHEN MATCHED THEN
UPDATE SET (DO YOUR UPDATES)
WHEN NOT MATCHED THEN
(INSERT YOUR NEW ROWS)
Google MERGE syntax for more about the statement.
Just use MINUS.
query_1
MINUS
query_2
In your case, if you really want to use PL/SQL, then select count into a local variable. Write a logic, if count > 0 then do other stuff.

Resources