Compare several records in the same table
Hello, I m writing a bat file so I can export information that I question.I have an automatic integration which checks from time to time if there is no new client´s and generates a trigger.
If an error levonorgestrol I the process is repeated until the client is located in the database.
I can't create tables or change the structure.
My problem is that I need to compare the record more updated with the CLIENT_ID field
Let me give you an example:
Table A
ERROR_MESSAGE CREATEDUSERID CLIENT_ID
0 success 01/01/2009
1 error 01/01/2009
2 success 01/01/2009
1 success 01/02/2009
3 success 01/02/2009
4 error 01/02/2009
I need to compare "1" client created on 01/01/2009 this error gived with new records to see if there's success and my
query should only return the client_ID '4' and export with sqlplus
Could you please help me?
Hello
It works very well with my data.
What data do you use? After a few lines of sample data (CREATE TABLE AS executable... or INSERT... statements are best) that give you wrong results.
Make sure that everything is spelled correctly.
For example, roughly halfway through the query, you say
and io.status = 'Erro'
and at the end you say
having max(decode(io.status, 'Error', 1, 2)) ...
That is the same ("Error" or "Error") in both places?
Tags: Database
Similar Questions
-
Compare two rows in the same table
Hi all
I need to compare two rows in the same table, I don't know hoe to do it in pl/sql. Please help me on this.
example:
price of ro TR
xya0001 AMA.7 12
xya0003 ama6 14
xya0004 AMA.7 16
in table b is a unique value for each line, I need to compare the price column and see if the first value is less than or greater than the following value and, if there is more to put the corresponding value of br to a variable, and if it is less, put the corresponding value of the br to another variable. I don't know a method to do this, as I'm new to pl/sql. Please help me in thisfor data in(select tr,br,price, lag(price) over ( order by tr) newcol from yourtable) loop if nvl(data.newcol,0) > data.price then variable1:=data.br; else varable2:=data.br; end if; end loop;
-
Update of several rows of the same table using trigger
Hello
I have the following table
---------------------------------------------------------------------------------------------------------
CREATE TABLE ATT_PERMISSIONS ( DATE OF PRMONTHYR, EMPID NUMBER (10) DEFAULT VALUE 0 , FROMDATETIME DATE , TODMTFDATETIME DATE , NUMBER OF PERMMINS (3), SSLINASTRETCH NUMBER (3).
PERMATTINELIGIBLE NUMBER (1) DEFAULT VALUE 0 ); ------------------------------------------------------------------------------------------------------
I want to update the column PERMATTINELIGIBLE in the same table when each line is inserted.
I created following trigger for that
CREATE OR REPLACE TRIGGER SSL_AFTER_UPDATE AFTER UPDATE ON ATT_PERMISSIONS
FOR EACH LINE
BEGIN
Update ATT_PERMISSIONS set PERMATTINELIGIBLE =
(
SELECT THE CHECK BOX
WHEN PERMMINS < = 60
or
(
PERMMINS < = SSLINASTRETCH
AND
COUNT (CASE WHEN PERMMINS = 0 THEN NULL OTHERWISE 1 END)
COURSES (PARTITION BY empid, prmonthyr) < = 1
)
THEN 1
0 OTHERWISE
END
FROM ATT_PERMISSIONS where prmonthyr =:new.prmonthyr, empid =:new.empid
) where prmonthyr =:new.prmonthyr, empid =:new.empid;
END;
---------------------------------------------------------------------------------------------------
That is to say.
When a row is inserted the PERMATTINELIGIBLE of all records for this employee for the month is recalculated.
There would be only two or three records for an employee for a month.
PERMMINS is the number of minutes to use as short leave.
If it is less than 60 is eligible for the bonus to attendance for this employee.
If this isn't normally the date is not eligible for the attendance for this employee premium.
but an exception is that if a short leaves of DISP employee allowed a month (IE. ( SSLINASTRETCH ) in a straight line, IE. one day, that day is eligible for the bonus of presence.
That is to say. Why the number of short leaves in a month is taken. If it's one and done minutes use is lower to SSLINASTRETCH, then the day is eligible for the attendance bonus.
But when I try to create the trigger I get error, PL/SQL: ORA-00907: missing a right parenthesis.
Help, please
I use oracle 10g
I want to update the column PERMATTINELIGIBLE in the same table, when each row is inserted.
No - you do NOT want to do in a trigger.
You cannot query the same table that the trigger is activated on. -C' is a MUTANT and you will get an exception.
When a row is inserted is recalculated to the PERMATTINELIGIBLE of all the records for that employee for that month.
There would be only two or three records for an employee for a month.
PERMMINS is the number of minutes to use as short leave.
If it is less than 60 is eligible for the bonus to attendance for this employee.
If is not normally the day is not eligible for the premium of attendance for this employee.
but an exception is that if a short leaves of DISP employee allowed a month (IE. SSLINASTRETCH) in a straight line, IE. one day, that day is eligible for the bonus of presence.
That is to say. Why the number of short leaves in a month is taken. If it's one and done minutes use is less than SSLINASTRETCH, then the day is eligible for the bonus of presence.
But when I try to create the trigger I get error, PL/SQL: ORA-00907: missing a right parenthesis.
You can not do in a trigger. Even if correct you this error of syntax, you will get the exception that I mentioned above.
Oracle is a multi-user system. Other users can modify the same table that you use.
So even if you could try to interview "all the records for this employee" some of them may be locked if other users access them. Also, an insert statement could try to insert two rows for an employee and the trigger could not have seen the other lines still.
If you want to work with data SETS (all records for an employee), you must write the PL/SQL code that LOCKS all lines updated to prevent others to interfere.
Then you make your operations but NOT in a trigger. Perform inserts and then use an UPDATE query to set this value.
Then VALIDATE to unlock the lines.
-
comparing string values in the same table
I have a table with ID (14 digit string values) starting with "A". Ex: A21849B1020792. There may be a different ID with the same substring 'B1020792' in the same table, example: A12349B1020792. If this happens, the ID of the last creation date of must be returned in the result. In other words, comparison is done on the same table. "In the example provided, say A12349B1020792 has the date of 1 January 2015 'and A21849B1020792 has date February 1, 2015", since A21849B1020792 has the most recent date, the result must contain only A21849B1020792.
ID create_date
--- ------------------
A21849B1020792 02/01/2015
A12349 B1020792 01/01/2015
A12345B1234567 01/03/2015
A43567B1234567 01/04/2015
Here's the query I used:substr (a.id, 7) select sc, max (a.create_date) cd1
of table a, table b
where a.id! = b.id
and substr (a.id, 7) = substr (b.id, 7)
and a.id like 'A %' and b.id like 'A %' / * (I used a % because I am interested only IDs that begin with A) * /.
and a.id ('A12349B1020792', 'A21849B1020792','A12345B1234567 ','A43567B1234567')
Group of substr (a.id, 7);
Result:
---------
sc cd1
--- ------
B1020792 02/01/2015
B1234567 04/01/2015
There is one thing that I'm not able to do with my request, that is, be able to get real IDs list and not list of substrings. If I added a.id column in the select clause and the group by clause, the query includes both by substr (a.id, 7) and a.id and as a result, the query returns four lines as below:
substr (a.id, 7) select sc, max (a.create_date) cd1, a.id ID
of table a, table b
where a.id! = b.id
and substr (a.id, 7) = substr (b.id, 7)
and a.id like 'A %' and b.id like 'A %' / * (I used a % because I am interested only IDs that begin with A) * /.
and a.id ('A12349B1020792', 'A21849B1020792','A12345B1234567 ','A43567B1234567')
Group by substr (a.id, 7), a.id.
Result:
---------
sc cd1 ID
--- ------ ---------
01/01/2015 A12349B1020792 B1020792
B1020792 01/02/2015 A21849B1020792
B1234567 01/03/2015 A12345B1234567
B1234567 01/04/2015 A43567B1234567
My goal is to retrieve a list of all the unique identifiers that have the later dates.
ID:
---
A21849B1020792
A43567B1234567
Thanks for your time in advance!
Hello
user11951344 wrote:
I have a table with ID (14 digit string values) starting with "A". Ex: A21849B1020792. There may be a different ID with the same substring "B1020792" in the same table, example: A12349B1020792. If this is the case, the ID of the last creation date must be returned in the result. In other words, comparison is done on the same table. "In the example provided, say A12349B1020792 updated 1 January 2015 ' and A21849B1020792 a date February 1, 2015", as A21849B1020792 has the most recent date, the result should contain only the A21849B1020792.
ID create_date
--- ------------------
A21849B1020792 02/01/2015
A12349B1020792 01/01/2015
A12345B1234567 03/01/2015
A43567B1234567 01/04/2015
Here's the query I used:
substr (a.id, 7) select sc, max (a.create_date) cd1
of table a, table b
where a.id! = b.id
and substr (a.id, 7) = substr (b.id, 7)
and a.id like 'A %' and b.id like 'A %' / * (I used a % because I am interested only IDs that begin with A) * /.
and a.id ('A21849B1020792', 'A12345B1234567', 'A12349B1020792', 'A43567B1234567')
Group of substr (a.id, 7);
Result:
---------
sc cd1
--- ------
B1020792 02/01/2015
B1234567 01/04/2015
There is one thing that I am not able to do with my request, that is, the ability to retrieve the list of IDs real and not a list of substrings. If I added a.id column in the select clause and the group by clause, the query groups according to the two substr (a.id, 7) and a.id and as a result, the query returns four lines as below:
substr (a.id, 7) select sc, max (a.create_date) cd1, a.id ID
of table a, table b
where a.id! = b.id
and substr (a.id, 7) = substr (b.id, 7)
and a.id like 'A %' and b.id like 'A %' / * (I used a % because I am interested only IDs that begin with A) * /.
and a.id ('A21849B1020792', 'A12345B1234567', 'A12349B1020792', 'A43567B1234567')
Group by substr (a.id, 7), a.id.
Result:
---------
sc cd1 ID
--- ------ ---------
B1020792 01/01/2015 A12349B1020792
B1020792 02/01/2015 A21849B1020792
B1234567 03/01/2015 A12345B1234567
B1234567 01/04/2015 A43567B1234567
My goal is to retrieve a list of all the unique identifiers that have the later dates.
ID:
---
A21849B1020792
A43567B1234567
Thanks for your time in advance!
If it makes sense to treat the first 6 characters of the ID separately from the rest of the id in this problem, maybe it makes sense to store those two parts of the id in two different columns. Relational databases work best when each column of each row contains 1 single piece of data (at most). It is so fundamental to the design of table that he called the first normal form.
Given that the two parts are stored in column 1, so you can something like this Request Top - N:
WITH got_r_num AS
(
SELECT r.id
r.create_date AS cd1
, RANK () OVER (PARTITION OF SUBSTR (r.id, 7))
ORDER BY r.create_date DESC
) AS r_num
FROM table_x g - g for data values
JOIN table_x r - r for related values
ON SUBSTR (r.id, 7) = SUBSTR (g.id, 7)
WHERE g.id IN ('A12349B1020792'
, "A21849B1020792".
, "A12345B1234567".
, "A43567B1234567".
)
- AND g.id LIKE 'A %' - if necessary. The above condition ensures already g.id start with "A".
AND r.id LIKE 'a % '.
)
SELECT id, create_date
OF got_r_num
WHERE r_num = 1
;
If you would care to post CREATE TABLE and INSERT instructions for the sample data, and then I could test this.
-
Several updates on the same Table
Hello
I need to update the same Table (T1) but to filter for different conditions as below.
and I have a set of filter conditions 50 and update the same table.
Is it possible to get in the single update statement?
BEGIN
UPDATE T1
SET PTA_CLM_CNT = v_PTA_CLM_CNT__77,
PTB_CLM_CNT = v_PTB_CLM_CNT__77
WHERE run_dt = v_in_parm_dt
AND state_code = '77';
EXCEPTION
WHEN NO_DATA_FOUND THEN
null;
WHILE OTHERS THEN
v_error_cd: = SQLCODE;
v_error_msg: = SUBSTR (SQLERRM, 1, 200);
v_curr_record: = 'sum_bene_state_updt_77 ' | v_in_parm_dt;
create_error_log ('_sum_cnts', v_error_cd, v_error_msg, v_curr_record);
END;
BEGIN
UPDATE T1
SET PTA_CLM_CNT_ = v_PTA_CLM_CNT__78,
PTB_CLM_CNT_ = v_PTB_CLM_CNT__78
WHERE run_dt = v_in_parm_dt
AND state_code = '78';
EXCEPTION
WHEN NO_DATA_FOUND THEN
null;
WHILE OTHERS THEN
v_error_cd: = SQLCODE;
v_error_msg: = SUBSTR (SQLERRM, 1, 200);
v_curr_record: = 'sum_bene_state_updt_78 ' | v_in_parm_dt;
create_error_log ('sum_cnts', v_error_cd, v_error_msg, v_curr_record);
END;
Thanks in advanceMay be trying to use DECODE
UPDATE T1 SET PTA_CLM_CNT = DECODE(state_code,77,v_PTA_CLM_CNT_77,78,v_PTA_CLM_CNT_78) PTB_CLM_CNT = DECODE(state_code,77,v_PTB_CLM_CNT_77,78,v_PTB_CLM_CNT_78) WHERE run_dt = v_in_parm_dt;
-
Compare the records from the same table
Hi all
I have this sample, the data were rendered are group by my_id, the same number of records exist for the two my_ids (2 and 62) as:
create table myTest as ( my_id number, fieldA varchar2, fieldB number, fieldC number, cost_A number, cost_B number ) insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B) values (2, 'MAINT', 60, 5, 10.3, 15.00); insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B) values (2, 'MAINT', 70, 6, 20.3, 25.00); insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B) values (2, 'MAINT', 80, 7, 30.3, 35.00); insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B) values (62, 'MAINT', 60, 5, 10.3, 5.00); insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B) values (62, 'MAINT', 70, 6, 20.3, 25.00); insert into myTest (my_id, fieldA, fieldB, fieldC, cost_A, cost_B) values (62, 'MAINT', 80, 7, 10.3, 15.00);
How can display the records with numbers of different costs for the same unique key (fieldA, fieldB and FieldC)?
With the help of Oracle Database 11 g Enterprise Edition Release 11.2.0.4.0.
I hope that my message is clear.
Thank you!
Hello
user9542267 wrote:
... I need all the exits...
Me too. Always show the complete, accurate, results you want from the data provided. If you want 2 rows at the exit point, don't tell you want 1.
Here are the results you want?
MY_ID FIELDA, FIELDB, FIELDC OLD_COST_A COST_A OLD_COST_B COST_B
------ ---------- ------ ------ ---------- ------ ---------- ------
MAINT 62 60 5 10.3 10.3 15 5
MAINT 62 80 7 30.3 10.3 35 15
If so, here's a way to get them:
WITH got_old_costs AS
(
SELECT fielda, fieldb, fieldc, cost_a, my_id, cost_b
, LAG (cost_a) OVER (PARTITION BY fielda, fieldb, fieldc)
ORDER BY my_id
) AS old_cost_a
, LAG (cost_b) OVER (PARTITION BY fielda, fieldb, fieldc)
ORDER BY my_id
) AS old_cost_b
OF mytest
WHERE my_id IN (2, 62)
)
SELECT my_id, fielda, fieldb, fieldc
old_cost_a, cost_a
old_cost_b, cost_b
OF got_old_costs
WHERE my_id = 62
AND (cost_a <> old_cost_a)
OR cost_b <> old_cost_b
)
ORDER BY fielda, fieldb, fieldc
;
-
Selection of records in the same table problem
Hi all
I have the following table
My results should bewith emp as ( select '11' A,'aaa' B, '40' C from dual union all select '11','bbb', '40' from dual union all select '33','ccc', '30' from dual union all select '44','ddd', '20' from dual union all select '11','eee', '10' from dual )
Basically the requirement is select different combination of values in the column 'B' need to know what is the value of 'A' (it must be the same for all records of three)A B C 11 aaa 40 11 bbb 40 11 eee 10
Hope in this sense.
See you soon
Sexyan attempt to rephrase:
you want all the files "aaa", "bbb" and "EEA" where, for each set of column A, where they exist.
that is, if games to have only "aaa" and "bbb" but not "eee" in column B, then you don't want to retrieve this record?
Try this:
SQL> with emp as ( 2 select '11' A,'aaa' B, '40' C from dual union all 3 select '11','bbb', '40' from dual union all 4 select '33','ccc', '30' from dual union all 5 select '44','ddd', '20' from dual union all 6 select '11','kkk', '20' from dual union all 7 select '10','eee', '20' from dual union all 8 select '11','eee', '10' from dual 9 ) 10 select a, b, c 11 from (select emp.*, count(*) over (partition by a) cnt 12 from emp 13 where b in ('aaa','bbb','eee')) 14 where cnt = 3 15 / A B C -------------------------------- -------------------------------- -------------------------------- 11 eee 10 11 aaa 40 11 bbb 40 3 rows selected.
Published by: WhiteHat June 30, 2011 11:04
-
problem of query by comparing records from the same table against each other
Hi, i'ms truggling to see where I am going wrong here. I have data from cards of following music: -.
italic , I need to identify the rogue data where the highest_position is incorrectly registered lower or 0 as therewith charts_table as (select 1 id,'oasis' product_name,0801 week, 20 week_chart_position, 20 highest_chart_position from dual union select 2,'oasis',0802,14,14 from dual union select 3,'oasis',0803,17,14 from dual union select 4,'oasis',0804,29,14 from dual union select 5,'kings of leon',0801,55,55 from dual union select 6,'kings of leon',0802,22,22 from dual union select 7,'kings of leon',0803,35,22 from dual union select 8,'kings of leon',0804 , 11 , 11 from dual union select 9,'bob dylan',0801, 88 , 88 from dual union select 10,'bob dylan',0802,79 , 79 from dual union select 11,'bob dylan',0803,54,54 from dual union select 12,'bob dylan',0804,22,22 from dual union select 13,'kid rock',0801,19,19 from dual union select 14,'kid rock',0802,24,19 from dual union select 15,'kid rock',0803,55,0 from dual union select 16,'kid rock',0804,44,0 from dual)
position of the highest week was at an earlier date. (stick with me) for example in this kid of rock data initially charted at position 19. What follows
week (0802) his week_chart_position slipped to 24 and the position the highest card is correctly saved at 19 (ie the week 1). The problem
is available in week 0803 and 0804 when somehow the data was corrupted and the higest_chart_position is trying to read 0 (instead of 19). How can I write a query to identify this error?
is the best I have at the moment:-
but it's not working. can someone help and tell where I'm wrong. Thanks a lot for the research/resolve.select * from charts_table ct1 where nvl2(highest_chart_position,0,99999)> (select min(week_chart_position) from charts_table ct2 where ct2.week<ct1.week and ct1.product_name=ct2.product_name)
Ahh, I was suggesting using the lag function to ensure that highest chart position is not fall lower than highest prior chart positions. Your concern is he reports a higher chart position that she never had. In this case I use the MIN function as an analytic function rather than as an aggregate function:
with t1 as ( select ct.* , min(week_chart_position) over( PARTITION by product_name order by week) highest from charts_table ct order by product_name, week ) select * from t1 where highest != highest_chart_position;
Conversely, you place your order of cards, which is the highest rank is 1, thew me. I saw the high word and the expected value in the highest chartposition column to be on the rise, but has seen examples of it decreases. It was an oversight on my part.
Published by: Sentinel on October 9, 2008 08:31
-
COMPARING TWO ROWS IN THE SAME TABLE AND DELETE THEM USING DELETION BLOCK
Hi all
I have a problem. I have an Employee table
employee
Emp_name Emp_id Emp_status Emp_date
John 1 P 11-27-2010
Mike 2 26/11/2010 S
Simon P 3 22 / 11 / 2010
Dennis C 4 25/11/2010...
I compare the rows in the table and their employee_status if 'P', should I remove the... Here, I should delete lines 1 and 3 as their Emp_status is 'P '. I only use BULK DELETE... I tried to use the join query internal and deletion, but it does not work... Can someone help me...Ooops, it should be:
and e2.emp_status in ('P', 'C')
not
and e2.emp_status in ('P', 'A')
Here it goes:
SQL> select * 2 from employees 3 / E EMP_NAME EMP_KEY X - ---------- ---------- ---------- C John 123 1 P Mike 123 2 P Paul 123 6 C Harry 124 5 A Gass 125 7 C Gass 125 7 6 rows selected. delete employees e1 where 2 = ( select count(distinct e2.emp_status) from employees e2 where e2.emp_key = e1.emp_key and e2.emp_status in ('P','C') ) / 3 rows deleted. SQL>
SY.
-
parents and children of the same table records
I want to create a query that is a union such that 2nd resultset is based on the game of results 1. I have a table that has parent and child records in the same table.
Table: EVENTS
EVENT_ID
PARENT_EVENT_ID
CREATED_DATE
(other columns)
If PARENT_EVENT_ID is null, then this is a parent record, otherwise it is a book of the child. I want to select all parent records then union them with all the related child records... something like this:
Select * from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null - all parents
Union
Select * event where PARENT_EVENT_ID in (select EVENT_ID of EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null)-include all children selected from the top parent
It works but it of kind of ugly, I want to avoid using the subselect in the 2nd because it is a repeat of the 1st statement, is there a way to alias the first statement and only refer to him in the 2nd query?Hello
CONNECTION is very similar to a UNION on the same table. How is this:
SELECT * FROM events WHERE created_date < SYSDATE - 90 START WITH parent_event_id IS NULL CONNECT BY parent_event_id = PRIOR event_id AND LEVEL <= 2 -- Maybe ;
?
To answer your question: Yes, you can still keep the result set of a query and the Treaty as if it were a table or vew, like this:
WITH roots AS ( SELECT * FROM events WHERE created_date < SYSDATE - 90 AND parent_event_id IS NULL ) SELECT * FROM roots -- UNION ALL -- SELECT * FROM events WHERE created_date < SYSDATE - 90 AND parent_event_id IN ( SELECT event_id FROM roots ) ;
It's always a bit ugly, however. I would use CONNECT BY or from Oracle 11.2, a recursive WITH clause (which is explicitly a UNION), or, in any version, a self-join, like this:
SELECT c.* FROM events c LEFT OUTER JOIN events p ON p.event_id = c.parent_event_id WHERE p.parent_event_id IS NULL ;
I hope that answers your question.
If not, post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and the results desired from these data.
Report where the queriy above (the one you decide to use) is producingthe bad results and explain, using specific examples, how you get the right results from these data in these places.
Always say what version of Oracle you are using (for example, 11.2.0.2.0).
See the FAQ forum {message identifier: = 9360002} -
Copy a few lines in the same table, but with different IDS
Hi all
I had this problem... I have a table with its pk (which is a sequence). With a statement select, I extract a few lines, and then I would record in the same table, but with IDS different (obviously eheheh).
I don't know how to do...
Thks for all help ;)
p.s. I want to change some fields in lines before saving as well... don't know how do it more...
example:
ID NAME TYPE
1 ferrari car
2 cars lamborghini
I'm trying to copy the second and changing its type in "BMW".
I could result:
1 ferrari car
2 cars lamborghini
3 bmw cars
Thank youAssuming that your table is named cars_tb and the cars_seq of the sequence.
insert into cars_tb select cars_seq.nextval, name, type from cars_tb
This would overlap with any existing lines, but with a new id.
If you want to duplicate a specific line and change a value, you could do something like:
insert into cars_tb select cars_seq.nextval, name, 'BMW' from cars_tb where type = 'lamborghini'
or better, use the PK
insert into cars_tb select cars_seq.nextval, name, 'BMW' from cars_tb where id = 2
-
Updated with the values in the same table, for other records corresponding to conditions
Hi Experts,
Sorry do not provide the structure of the table (it is a simple structure)
I have a requirement where I need to update the columns of a table based on the same table with some match empid and date values. If the date and empid match so I have these values to any other folder and update of one who is not having details of Office . I need the Update query
Before the update my array of values is as below
Sort_num Emp_id Bureau start_date
1 101 AUS 01/01/2013
2 101 01/01/2013
3 101 15/01/2013
4 103 USA 01/05/2013
5 103 01/01/2013
6 103 05/01/2013
7 104 FRA 01/10/2013
8 104 10/01/2013
9 104 01/01/2013After update my table should be as below
Sort_num Emp_id Bureau start_date
1 101 AUS 01/01/2013
2 101 AUS 01/01/2013
3 101 15/01/2013
4 103 USA 01/05/2013
5 103 01/01/2013
6 103 USA 01/05/2013
7 104 FRA 01/10/2013
8 104 FRA 01/10/2013
9 104 01/01/2013Thanks in advance
I don't have the time to create the table with the data, but basically, you should be able to code the following text
update one table
Office set = (select desktop in table b where b.emp_id = a.emp_id)
and b.start_date = a.start_date
and b.office is not null
)
where is ([as well as overall query])
and a.office is null
In my opinion, who will do the trick.
HTH - Mark D Powell.
-
Comparing the same tables of two schemas
I had already posted a similair on this thread. So, I thought I would start a new thread on this to avoid confusion
I want to generate a report that shows the difference in the data type of the columns in the same table in two different patterns.
The query using LESS shows the difference below.
But, since it is a report, I want to see the form of results the two schemas that has column of different data types with the same names of tablecreate table SCOTT.mytab1 (empid number); create table HR.mytab1 (empid varchar2(34)); SELECT table_name, column_name, OWNER schema, data_type FROM dba_tab_cols where owner = 'SCOTT' AND TABLE_NAME = 'MYTAB1' MINUS SELECT table_name, column_name, OWNER schema, data_type FROM dba_tab_cols where owner = 'HR' AND TABLE_NAME = 'MYTAB1' Result: TABLE_NAME COLUMN_NAME SCHEMA DATA_TYPE --------------- --------------- ---------- ------------ MYTAB1 EMPID SCOTT NUMBER
Expected results:
Anyway I could do this?TABLE_NAME COLUMN_NAME SCHEMA DATA_TYPE --------------- --------------- ---------- ------------ MYTAB1 EMPID SCOTT NUMBER MYTAB1 EMPID HR VARCHAR2
Why not get a single line with two types of data:
select a.table_name, a.column_name, a.data_type schema1_type, b.data_type schema2_type from all_tab_columns a, all_tab_columns b where a.owner = 'SCOTT' and b.owner = 'HR' and a.table_name=b.table_name and a.column_name=b.column_name and a.data_type!=b.data_type;
This extracted single query columns present on the two schemas but with different data types. These columns only on one of the two schemas are not extracted.
Max
-
update to column values (false) in a copy of the same table with the correct values
Database is 10gr 2 - had a situation last night where someone changed inadvertently values of column on a couple of hundred thousand records with an incorrect value first thing in the morning and never let me know later in the day. My undo retention was not large enough to create a copy of the table as it was 7 hours comes back with a "insert in table_2 select * from table_1 to timestamp...» "query, so I restored the backup previous nights to another machine and it picked up at 07:00 (just before the hour, he made the change), created a dblink since the production database and created a copy of the table of the restored database.
My first thought was to simply update the table of production with the correct values of the correct copy, using something like this:
Update mnt.workorders
Set approvalstat = (select b.approvalstat
mnt.workorders a, mnt.workorders_copy b
where a.workordersoi = b.workordersoi)
where exists (select *)
mnt.workorders a, mnt.workorders_copy b
where a.workordersoi = b.workordersoi)
It wasn't the exact syntax, but you get the idea, I wanted to put the incorrect values in x columns in the tables of production with the correct values of the copy of the table of the restored backup. Anyway, it was (or seem to) works, but I look at the process through OEM it was estimated 100 + hours with full table scans, so I killed him. I found myself just inserting (copy) the lines added to the production since the table copy by doing a select statement of the production table where < col_with_datestamp > is > = 07:00, truncate the table of production, then re insert the rows from now to correct the copy.
Do a post-mortem today, I replay the scenario on the copy that I restored, trying to figure out a cleaner, a quicker way to do it, if the need arise again. I went and randomly changed some values in a column number (called "comappstat") in a copy of the table of production, and then thought that I would try the following resets the values of the correct table:
Update (select a.comappstat, b.comappstat
mnt.workorders a, mnt.workorders_copy b
where a.workordersoi = b.workordersoi - this is a PK column
and a.comappstat! = b.comappstat)
Set b.comappstat = a.comappstat
Although I thought that the syntax is correct, I get an "ORA-00904: 'A'. '. ' COMAPPSTAT': invalid identifier ' to run this, I was trying to guess where the syntax was wrong here, then thought that perhaps having the subquery returns a single line would be cleaner and faster anyway, so I gave up on that and instead tried this:
Update mnt.workorders_copy
Set comappstat = (select distinct)
a.comappstat
mnt.workorders a, mnt.workorders_copy b
where a.workordersoi = b.workordersoi
and a.comappstat! = b.comappstat)
where a.comappstat! = b.comappstat
and a.workordersoi = b.workordersoi
The subquery executed on its own returns a single value 9, which is the correct value of the column in the table of the prod, and I want to replace the incorrect a '12' (I've updated the copy to change the value of the column comappstat to 12 everywhere where it was 9) However when I run the query again I get this error :
ERROR on line 8:
ORA-00904: "B". "" WORKORDERSOI ": invalid identifier
First of all, I don't see why the update statement does not work (it's probably obvious, but I'm not)
Secondly, it is the best approach for updating a column (or columns) that are incorrect, with the columns in the same table which are correct, or is there a better way?
I would sooner update the table rather than delete or truncate then re insert, as it was a trigger for insert/update I had to disable it on the notice re and truncate the table unusable a demand so I was re insert.
Thank youHello
First of all, after post 79, you need to know how to format your code.
Your last request reads as follows:
UPDATE mnt.workorders_copy SET comappstat = ( SELECT DISTINCT a.comappstat FROM mnt.workorders a , mnt.workorders_copy b WHERE a.workordersoi = b.workordersoi AND a.comappstat != b.comappstat ) WHERE a.comappstat != b.comappstat AND a.workordersoi = b.workordersoi
This will not work for several reasons:
The sub query allows you to define a and b and outside the breakets you can't refer to a or b.
There is no link between the mnt.workorders_copy and the the update and the request of void.If you do this you should have something like this:
UPDATE mnt.workorders A -- THIS IS THE TABLE YOU WANT TO UPDATE SET A.comappstat = ( SELECT B.comappstat FROM mnt.workorders_copy B -- THIS IS THE TABLE WITH THE CORRECT (OLD) VALUES WHERE a.workordersoi = b.workordersoi -- THIS MUST BE THE KEY AND a.comappstat != b.comappstat ) WHERE EXISTS ( SELECT B.comappstat FROM mnt.workorders_copy B WHERE a.workordersoi = b.workordersoi -- THIS MUST BE THE KEY AND a.comappstat != b.comappstat )
Speed is not so good that you run the query to sub for each row in mnt.workorders
Note it is condition in where. You need other wise, you will update the unchanged to null values.I wouold do it like this:
UPDATE ( SELECT A.workordersoi ,A.comappstat ,B.comappstat comappstat_OLD FROM mnt.workorders A -- THIS IS THE TABLE YOU WANT TO UPDATE ,mnt.workorders_copy B -- THIS IS THE TABLE WITH THE CORRECT (OLD) VALUES WHERE a.workordersoi = b.workordersoi -- THIS MUST BE THE KEY AND a.comappstat != b.comappstat ) C SET C.comappstat = comappstat_OLD ;
This way you can test the subquery first and know exectly what will be updated.
This was not a sub query that is executed for each line preformance should be better.Kind regards
Peter
-
Insert select on the same table: possible without side effects?
I have a very large table T1 containing millions of records. I need to treat its lines and create a few new lines based on selection.
Table T1 contains events and one of them, with the code 100, is created by the further development of other events inside the table.
My code is as follows:
insert /*+append */ into T1 (code,...) values (100, c1,c2,...) select c1,c2... from T1 where (code=20 or code=10) and <other conditions>...
as you can see I'm extract T1 lines to insert again in T1 with a different code and I use the direct path in order to get good performance.
My fear is: choose is made from the same table I risk data loss? In general it is a good practice? Or is it better to create another table?
Hello
No I don't think that there may be loss of data. But that may depend on the behavior of the application and your where clause.
I will explain how it is treated, so you can see if it's ok:
1. the table is locked because of the insert add, (as)
2 lines are read by select and make compatible from the State that was at the beginning of the query - 1.
3 rows are inserted at the end, after the high-water line
4 columns for new lines are sorted to be merged in the index
5. high watermark is adjusted - visible new lines and lock is released
Note that 2. and 3. occur at the same time: rows are inserted all read.
Note that anyone can choose in the table during the operation - they see the changes committed only - if the State 1.
all other DML are waiting for the lock being released, and will see new ranks and then
If you have things that prevent the direct-path insert, append the hint will be ignored. So, if you must rely on close to 1. then the best lock explicitly with table lock. But I don't think that you need.
Kind regards
Franck.
Maybe you are looking for
-
Satellite P205D freeze after hibernation/standby in Win7
Hello Satellite P205D my daughter has just been upgraded to Windows 7. fine, except that now, it freezes shortly after the resumption from standby or hibernation. Minidumps just point to a "hardware". We executed the installed Toshiba installation so
-
The Adaptive brightness sensor issue
Hello I have an Aspire 571 V5 Windows 8 My screen never turns off automatically brightness adjust so it keeps dimming in light to dark every minute very annoying, I think that is because of the Adaptive brightness of sesnor on the laptop. I went to t
-
How can I get a phone number or on line help chat?
How can I get a phone number or on line help chat?
-
Unable to play Microsoft Dungeon Siege II error: Exception caught
Original title: Microsoft Dungeon Siege II exception caught While trying to start the game Dungeon Siege II, I get an error that says: Exception detectedThe exception data collection, it may take a minute.Then, after a few seconds, it says 'Done '.an
-
After the update of Lollipop gyroscope no longer works... You have any suggestions?