Deleting from a table based on values in a second table
Is it possible to DELETE from (or for that matter do an UPDATE to) a table based on values in another table? I have gone through the online documentation but can't seem to find anything. I'm trying to delete rows from table A where A.field1 = B.field1 and B.field2 = 'X'. (B being the second table)
It is done using subqueries in the where clause.
delete from A
where A.field1 in (select B.field1 from B where B.field2 = 'X');
delete from A
where exists (select 'x' from B where B.field1 = A.field1 and B.field2 = 'X');
delete from A
where A.rowid in (select A.ROWID from from A, B where B.field1 = A.field1 and B.field2 = 'X');
And many other varieties. Eg. more specialised:
delete from A
where A.txdate < (select B.prune_date FROM B where B.field1 = A.field1 and B.field2 = 'X')
and A.txstate in (select S.txstate from S where S.prodlass=a.prodclass and s.deletable='Y');
Similar Messages
-
How to exclude records from one table that is contained in a second table
I am trying to create a Crystal report that excludes records from one Table that is contained in a second table using the != link option and it is not working. I've tried all of the different enforce options, and it is still not excluding those records. Does anyone have any suggestions of what I'm doing wrong or any other suggestions how I can obtain the results I need?
Thanks in advance!Have you tried by Command ?
Thanks,
Gordon -
Deleting rows from table based on value from other table
Hello Members,
I am struck to solve the issue said below using query. Would appreciate any suggestions...
I have two tables having same structures. I want to delete the rows from TableA ( master table ) with the values from TableB ( subset of TableA). The idea is to remove the duplicate values from tableA. The data to be removed are present in TableB. Catch here is TableB holds one row less than TableA, for example
Table A
Name Value
Test 1
Test 1
Test 1
Hello 2
Good 3
TableB
Name Value
Test 1
Test 1
The goal here is to remove the two entries from TableB ('Test') from TableA, finally leaving TableA as
Table A
Name Value
Test 1
Hello 2
Good 3
I tried below queries
1. delete from TestA a where rowid = any (select rowid from TESTA b where b.Name = a.Name and a.Name in ( select Name from TestB ));
Any suggestions..
We need TableB. The problem I mentioned above is part of process. TableB contains the duplicate values which should be deleted from TableA. So that we know what all values we have deleted from TableA. On deleted TableA if I later insert the value from TableB I should be getting the original TableA...
Thanks in advancedrop table table_a;
drop table table_b;
create table table_b as
select 'Test' name, 1 value from dual union all
select 'Test' ,1 from dual;
create table table_a as
select 'Test' name, 1 value from dual union all
select 'Test' ,1 from dual union all
select 'Test' ,1 from dual union all
select 'Hello' ,2 from dual union all
select 'Good', 3 from dual;
/* Formatted on 11/23/2011 1:53:12 PM (QP5 v5.149.1003.31008) */
DELETE FROM table_a
WHERE ROWID IN (SELECT rid
FROM (SELECT ROWID rid,
ROW_NUMBER ()
OVER (PARTITION BY name, VALUE
ORDER BY NULL)
rn
FROM table_a a
WHERE EXISTS
(SELECT 1
FROM table_b b
WHERE a.name = b.name
AND a.VALUE = b.VALUE))
WHERE rn > 1);
select * from table_a
NAME VALUE
Test 1
Hello 2
Good 3Edited by: pollywog on Nov 23, 2011 1:55 PM -
Select from different tables based on value
Hi Forum,
I'm stuggling with a problem:
In a table, I have two columns, pref_type and pref_value. Column pref_type stores a number value that is linked to a preference type, whereas column pref_value stores the value of that preference.
Unfortunately, the data model is such that per preference type a different tables needs to be used, so for example, if
pref_type = 1, table to query is A, whereas if
pref_type = 2, table to query is B.
I now want to write a query that resolves the preferences, ie it should branch to the respective table based on pref_type and search for pref_value and deliver back a different column of that respective table.
My first approach was like that:
select decode (pref_value, 1, (select result from A where pref_value = ???),
2, (select result from B where pref_value = ???)
where the three ??? depict the problem I'm having, as Oracle doesn't allow me to have a harmonized subquery here.
How do I achieve this?
Thanks for any help,
J. SiebenI believe this is what you are trying to get at:
SELECT DECODE(m.pref_type, 1, (SELECT result FROM a
WHERE a.pref_value = m.pref_value),
2, (SELECT result FROM b
WHERE b.pref_value = m.pref_value)
FROM my_table mAlthough, if the tables are large, that might be pretty slow. Better than an outer join, but one of those correlated queries is going to be run for each row in my_table. You could lso try something like:
SELECT m.pref_type, r.result
FROM my_table m,
(SELECT 'A' tbl, pref_value, result
FROM a
UNION ALL
SELECT 'B', pref_value, result
FROM b) r
WHERE DECODE(m.pref_type, 1, 'A', 'B') = r.tbl and
m.pref_value = r.pref_valueHTH
John -
Update table based on values from other table
Hi,
I am trying to update one table based on the values of another table. Since you can't use From in update statements, how do you execute this?
For example i have to tables, Table A and Table B. I want to update a column or columns in Table A based on another value in Table B:
So if the column in Table B was 1 then column in Table A would be Yes, if Table B was 2, then Table A would be Yes, if Table B was 3 then Table A would be N/A and so on...
Any help would be appreciated.
thanks,
scottSQL> select * from t1;
ID ST
1
2
3
SQL> select * from t2;
NO
1
2
3
4
SQL> update t1 set status=(select decode(no,1,'Y',2,'N','NA') from t2 where t1.id=t2.no);
3 rows updated.
SQL> select * from t1;
ID ST
1 Y
2 N
3 NA
Daljit Singh -
Update 1 column,1 single table based on where results of multiple tables
I would like the my_id column in 1 table updated to the static value of 247 for my_id stored in the 1 single table based on the where clause, which uses the my_id column in it.
The update statement updates all rows in table1, instead of just the rows where
the condition ( (b.my_id=a.my_id)
and ( b.my_name like 'OIS SrClerk%')
and (a.f_id=m.f_id)
and (trunc(m.cr_time) < '02-Apr-2008')) is true
What needs to be changed?
update table1 a
set a.my_id=247
from table1 a, table2 b, table3 m
where
(b.my_id=a.my_id)
and ( b.my_name like 'OIS SrClerk%')
and (a.f_id=m.f_id)
and (trunc(m.cr_time) < '02-Apr-2008')Are you looking for this?
UPDATE table1 A
SET A.my_id = 247
WHERE EXISTS (SELECT 'x'
FROM table2 b,
table3 M
WHERE b.my_id = A.my_id
AND b.my_name LIKE 'OIS SrClerk%'
AND A.f_id = M.f_id
AND TRUNC (M.cr_time) < to_date('02-apr-2008 00:00:00','dd-mon-yyyy HH:MI:SS') )changed date string to to_date
Message was edited by:
devmiral -
Hello,
I was wondering if it's possible have a matrix which shows total credits by month and have drill-down groupings to display the invoice numbers for each credit. Then, once the drill-down is expanded for a particular month, be able to click on the invoice
number and have another table under the matrix display the details of the invoice.
I don't really want a drill-through because that opens a different report in the window, what I want to be able to do is stay on the main report. I also don't want to drill-into the invoice details from the matrix as that expands the matrix out and
makes it unruly and ugly. I've been playing with subreports but not able to get what I want out of it.
I hope this makes sense - is there a way to do this in SSRS?
Thanks!!Hi AvenueStuart,
After testing the issue in my environment, we can refer to the steps below to work around the issue:
Create a new table with the details of the invoice under the matrix.
Add a parameter named invoice which get available values from the invoice field in the dataset.
Right-click the [Sum(invoice)] cell to add an action with “Go to report” option, and specify the same report with the parameter below:
Name: invoice
Value: [invoice]
Right-click the table to add a filter in the table:
Expression: [invoice]
Operator: =
Value: [@invoice]
If there are any misunderstanding, please elaborate the issue for further investigation.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
Populating values in dropdown based on values selected in second dropdown.
Hi,
I have two listboxes on the screen. Based on value chosen in first dropdown, I want to populate the values to be selected in second dropdown. How can i achieve this? I would be glad if anyone can help me out with this requirement.
Thanks,
Ameet<font color=blue>Hi,
<li> Place 2 input fields on screen.
<li>Set Listbox property as it is shown in the below link.
<a href="http://2.bp.blogspot.com/_O5f8iAlgdNQ/SmPSPnRa0tI/AAAAAAAAFCQ/wW_URP-ld6o/s1600-h/drop_down-770536.JPG" target="_new">link to dropdown setting</a>
<li>Set function code to first dropdown box to trigger event when you select value from first dropdown box. So that second one is changed.
<li>Declare variable like below in TOP include of the module pool program.
<pre>DATA: g_drop_1 TYPE char10,
g_drop_2 TYPE char10,
ucomm TYPE sy-ucomm.
TYPE-POOLS vrm.
DATA: name TYPE vrm_id,
list TYPE vrm_values,
value LIKE LINE OF list.</pre>
<li> Use screen flow logic of the screen like below
<pre>"PROCESS BEFORE OUTPUT.
PROCESS BEFORE OUTPUT.
field g_drop_1 MODULE drop_down_1.
field g_drop_2 MODULE drop_down_2.
"PROCESS AFTER INPUT.
PROCESS AFTER INPUT.
field g_drop_1.
field g_drop_2.</pre>
<li>Corresponding code for two drop down boxes.
<pre>&----
*& Module DROP_DOWN_1 OUTPUT
MODULE drop_down_1 OUTPUT.
CLEAR: list[],g_drop_2.
name = 'G_DROP_1'.
value-key = '1'.
value-text = 'value_1'.
APPEND value TO list.
CLEAR value.
value-key = '2'.
value-text = 'value_2'.
APPEND value TO list.
CLEAR value.
value-key = '3'.
value-text = 'value_3'.
APPEND value TO list.
CLEAR value.
value-key = '4'.
value-text = 'value_4'.
APPEND value TO list.
CLEAR value.
CALL FUNCTION 'VRM_SET_VALUES'
EXPORTING
id = name
values = list.
ENDMODULE. " DROP_DOWN_1 OUTPUT
*& Module DROP_DOWN_2 OUTPUT
MODULE drop_down_2 OUTPUT.
CLEAR list[].
name = 'G_DROP_2'.
CASE g_drop_1.
WHEN 1 OR 2.
value-key = '1'.
value-text = 'text1'.
APPEND value TO list.
CLEAR value.
value-key = '2'.
value-text = 'text2'.
APPEND value TO list.
CLEAR value.
WHEN 3 OR 4.
value-key = '3'.
value-text = 'text3'.
APPEND value TO list.
CLEAR value.
value-key = '4'.
value-text = 'text4'.
APPEND value TO list.
CLEAR value.
ENDCASE.
CALL FUNCTION 'VRM_SET_VALUES'
EXPORTING
id = name
values = list.
ENDMODULE. " DROP_DOWN_2 OUTPUT</pre>
Thanks
Venkat.O</font> -
Using JOIN to two tables to find records NOT in second table...
I always seem to get these types of queries wrong and need a bit of help:
I have a table of relationships called MARKETER_ACCOUNT and another table of pending relationships called ACCOUNT_ENROLLMENT.
I have accounts that can be enrolled with a MARKETER. They enter the ACCOUNT_ENROLLMENT table as Pending and on a certain Effective Date the go active and then move to the MARKETER_ACCOUNT table. If a relationship ends, it can end by either enrolling with another Marketer or if no pending enrollment they are assumed to be going back to the parent company.
So I want to find all the records that came back to the parent company (i.e. they should not have a pending record in the ACCOUNT_ENROLLMENT table but have an end date in the MARKETER_ACCOUNT table:
Select * from MARKETER_ACCOUNT M
where M.account_no NOT IN (select E.account_no from ACCOUNT_ENROLLMENT E
where E.effective_date between '08-DEC-2009' and '07-JAN-2010')
and M.expiration_date between '08-DEC-2009' and '07-JAN-2010';
This query is still running and I'm not sure where I am going wrong. I want to say GIVE me all the accounts in the MARKETER_ACCOUNT table that have an expiration date between '12/07/2009' and '01/07/2010' and where those accounts are NOT pending in the ACCOUNT_ENROLLMENT table where the effective date is for the same period.
Any help would be greatly appreciated.
SeanYou could possibly try the NOT EXISTS version of the query:
SELECT *
FROM MARKETER_ACCOUNT M
WHERE NOT EXISTS
SELECT NULL
FROM ACCOUNT_ENROLLMENT E
WHERE M.ACCOUNT_NO = E.ACCOUNT_NO
AND E.EFFECTIVE_DATE BETWEEN TO_DATE('08-DEC-2009','DD-MON-YYYY') AND TO_DATE('07-JAN-2010','DD-MON-YYYY')
AND M.EXPIRATION_DATE BETWEEN TO_DATE('08-DEC-2009','DD-MON-YYYY') AND TO_DATE('07-JAN-2010','DD-MON-YYYY');Otherwise check out these threads:
{message:id=1812597}
{thread:id=863295} -
Deriving 1 Value from 2 Fields Based on Values in Fields
I have a query that needs to return 1 of my values based on the values from 2 fields from 2 tables. However, the value should only be returned in 1 field and one of the fields should be evaluated first. If this first evaluated field has a certain value ('Y') then the other field is ignored.
To be more clear, per the normal query below, what I want is to somehow, if possible, combine the case t2.el_contr and case t1.contr_type statements so that one field, say: mbr_contr_type_desc returns a value.
So that if the value of t2.el_contr is 'Y' then 'SUB CHILD' is returned. It is important that this field should be evaluated first (If the value of it is 'Y' then the other field evaluation can be ignored). If this is not 'Y', then t1.contr_type should be evaluated and the values as listed below also returned to mbr_contr_type_desc.
I know the following isn't correct, but it might better illustrate what I'm trying to do (the normal query is below this)
case t2.el_contr
when 'Y' then 'SUB CHILD'
case t1.contr_type
when 'A' then '1 PARTY MALE SUB'
when 'B' then '1 PARTY FEMALE SUB'
when 'C' then '2 PARTY MALE SUB'
when 'D' then '2 PARTY FEMALE SUB'
when 'F' then 'FAMILY MALE SUB'
when 'G' then 'FAMILY FEMALE SUB'
end as mbr_contr_type_desc
In an Access query I would do the following in a column:
iif([el_contr]='Y',"SUB CHILD',SWITCH([contr_type]="A","1 PARTY MALE SUB",[contr_type]="B" etc.))
*NORMAL QUERY*
select t1.ssn
t1.type,
t1.grp_id,
t1.contr_type,
t2.el_contr,
case t2.el_contr
when 'Y' then 'SUB CHILD'
end as mbr_contr_type_desc_2,
case t1.contr_type
when 'A' then '1 PARTY MALE SUB'
when 'B' then '1 PARTY FEMALE SUB'
when 'C' then '2 PARTY MALE SUB'
when 'D' then '2 PARTY FEMALE SUB'
when 'F' then 'FAMILY MALE SUB'
when 'G' then 'FAMILY FEMALE SUB'
end as mbr_contr_type_desc_1
from member_elig t1
inner join grp_master t2 on
t1.grp_id = t2.grp_idPerfect!
Thanks, didn't know that existed. Still learning my way around pl/sql..
One other (unrelated) question..when i hit the CTRL+LEFT ARROW* button in my SQL window, it will not tab back. This is really annoying! I have looked through PREFERENCES-EDITOR and can't seem to find any setting that will allow this. Do you know of any way to change this so I can tab backwards/fowards through code in the SQL window?
Thanks for any suggestions!
*Or CTRL+RIGHT ARROW for that matter.. -
Calling functions and inserting tables based on values entered
Hello Everyone,
I am creating a function as below:
create or replace function func(flags in number,Ctry in varchar2) return number
is
maxv number;
flagv number;
begin
flagv:=1;
select max(num) into maxv from A;
if flags =1 then
insert into A(num,nam) values(maxv+1,Upper(Ctry));
else
flagv:=0;
end if;
return flagv;
end;
The function takes two parameters-The first one will be either 0 or 1.The second one will be name of a country.
If the first parameter is 1 then we would insert the country name passed, to the table name A.If its 0 then no insertion occurs and the function would return a value 0.
On compiling the function I get a success!.
When I do a
SQL>select distinct func(0,'UK') from B;
it works well and returns 0
However when I do
SQL>select distinct func(1,'UK') from B;
I expect an output of 1 & also expect UK to be inserted as anew row in the table A.However It throws an error saying "ORA-14551: cannot perform a DML operation inside a query .."
It is very important for me to use select to call the function, as my application would fire a select with that function and based on the value entered would insert or not insert at the back end.
Is there any way out to do this??
variable temps number
exec :number :=func(1,'UK');
does work but I cant use this in my application.
Hope you can help! Thanks!create or replace function func(flags in number,Ctry in varchar2) return number
is
PRAGMA AUTONOMOUS_TRANSACTION;
maxv number;
flagv number;
begin
flagv:=1;
select max(num) into maxv from A;
if flags =1 then
insert into A(num,nam) values(maxv+1,Upper(Ctry));
COMMIT;
else
flagv:=0;
end if;
return flagv;
end;
Is the above changes in BOLD enough or I need to do something else too in order to incorporate the autonomous transaction??
I am not too familiar with autonomous transaction.Could you please suggest the changes I need if any more required??
Thanks a ton for your suggestions!
Message was edited by:
user579245
Message was edited by:
user579245 -
Updating TABLEA based on values in TABLEB
Hi: I have the two following tables. I want to write a query which updates fields in TABLE1 based on certain values in TABLE2
1. Update SEVERITY in TABLE1 with SVRT_CD in TABLE2 for all TABLE1.INCIDENT = TABLE2.TKT_NBR
2. Update ESCALATION in TABLE1 with ESCL_LVL in TABLE2 for all TABLE1.INCIDENT = TABLE2.TKT_NBR
3. If the TABLE2.MNS_STATUS is 5 then set TABLE1.INCIDENTSTATUS='Acls' for all TABLE1.INCIDENT = TABLE2.TKT_NBR
4. If the TABLE2.MNS_STATUS is 4 then set TABLE1.INCIDENTSTATUS='Bctk' for all TABLE1.INCIDENT = TABLE2.TKT_NBR
remedy@MDS> descr TABLE1;
USERID NOT NULL VARCHAR2(32)
INCIDENT NOT NULL VARCHAR2(64)
INCIDENTSTATUS NULL VARCHAR2(16)
SEVERITY NULL NUMBER
ESCALATION NULL NUMBER
ONSTRAINT user_pk PRIMARY KEY (USERID,INCIDENT)
netcool@RMDSP02> describe TABLE2;
TKT_NBR NULL NUMBER(10,0)
ESCL_LVL NULL NUMBER(2,0)
SVRT_CD NULL VARCHAR2(1)
MNS_STATUS NULL NUMBER(1,0)
CONSTRAINT tkt_pk PRIMARY KEY (TKT_NBR)I think this will do what I want in #1 and #2. Can someone confirm?
UPDATE TABLE1
SET (SEVERITY, ESCALATION) = (SELECT SVRT_CD,ESCL_LVL from TABLE2 where to_number(TABLE1.INCIDENT) = TABLE2.TKT_NBR)Not sure how do I do the #3 and #4.
Thanks
RaviFor point 3 and 4 use decode. and dont forget to use EXISTS in your update.
UPDATE table1
SET (severity,
escalation,
incidentstatus) = (SELECT svrt_cd,
escl_lvl,
DECODE(mns_status,5,'Acls',4,'Bctk')
FROM table2
WHERE TO_NUMBER(table1.incident) = TABLE2.TKT_NBR)
WHERE EXISTS (SELECT NULL
FROM table2
WHERE TO_NUMBER(table1.incident) = TABLE2.TKT_NBR) Thanks,
Karthick. -
Execute process chain step based on value in a custom table
HI,
I have a requirement where i need to execute a process chain step based in the value of a field in a custom table.
e.g. If ztable-zflag = 'X' then execute next step else stop.
I am trying to use the decision between multiple alternatives process type, but i guess we can only use formulas in it.
Also, i created a custom method and called it through the badi RSAR_CONNECTOR, but the same isnt working as expected.
Would appreciate inputs from the experts.Hi,
Thank you for your response.
I followed exactly what is written in the document. But when I execute the process chain with the Decision between Multiple Alternatives process type, it fails with an exception message.
Below is what I have done:
Below is the code that I entered in the method (Please note that ZPC_CONTROL is the table from which I need to check the value. This table contains 2 fields: Process Chain Name and Flag. My requirement is that when a particular process chain has the flag checked, then the process chain should move ahead):
Below is the GET method:
Finally, here is the formula that I have written in the Decision Between Multiple Alternatives process type:
And the event is Option 2 (which I assume will move the process chain forward if the flag is checked for the process chain ‘TEMP_TEST’)
Please note that the flag for process chain ‘TEMP_TEST’ is checked in the table ZPC_CONTROL.
Below is the error in the process chain:
Please let me know where am I going wrong here. Appreciate your help. -
Update vs Merge - Updating one table based on values in another
Hello
I have two tables lets say TAB_A and TAB_B. I altered table B to include a new column from table A
I wrote a merge statement as follows to merge the data
MERGE INTO TAB_A
USING TAB_B
ON (TAB_A.SECURITYPERSONKEY=TAB_B.SECURITYPERSONKEY)
WHEN MATCHED THEN
UPDATE SET TAB_A.PPYACCOUNT=TAB_B.PPYACCOUNT;
I know INSERT is for inserting new records
UPDATE to my knowledge is to modify currently existing records (loosely)
MERGE is one I rarely used, until this particular scenario.
The code works perfectly fine, but I was wondering how could I write an update statement? Or in this scenario should I even be using an update statement?The MERGE is good and performs well, although it malfunctions (sometimes updating data wrongly) if the destination is a view with INSTEAD OF triggers.
The UPDATE with similar performance to the MERGE depends on SECURITYPERSONKEY being a unique or primary key on TAB_B. It is:
update (select TAB_A.PPYACCOUNT a_PPYACCOUNT, TAB_B.PPYACCOUNT b_PPYACCOUNT
from TAB_A JOIN TAB_B on TAB_A.SECURITYPERSONKEY=TAB_B.SECURITYPERSONKEY)
SET A_PPYACCOUNT=B_PPYACCOUNT;
This can set multiple columns. It also currently doesn't work on a view with INSTEAD OF triggers.
Frank's update also works with multiple columns (eg also updating col2 and col2), with this syntax:
UPDATE tab_a
SET (ppyaccount, col2, col3) = (
SELECT ppyaccount, col2, col3
FROM tab_b
WHERE securitypersonkey = tab_a.securitypersonkey
WHERE EXISTS (
SELECT ppyaccount
FROM tab_b
WHERE securitypersonkey = tab_a.securitypersonkey -
How to fetch a data form root table based on values of dependent table?
My Dependent Table.
EmpId
projectId
Object Name
Object Type
Object Description
My Root Table:
projectId
Project Name
Project Description
Project Duration
Project Manager
starting date
the above are my 2 tables . if search with an object name i should get the name of all the projects related to that . how do i achieve this SAPUI5REPORT YCHATEST .
tables : mara.
data : begin of itab occurs 0.
include STRUCTURE mara.
data: end of itab.
select * from mara into table itab where matnr like 'S%'.
Maybe you are looking for
-
IBooks export to text - nothing get's exported...why?
Hi. I have created a small iBook with iBooks Author - with some text, pictures etc. I would like to export it to text - so trying to do so results in no text file created - nothing gets exported. Any idea why - and what to check? Best regards Jens
-
Dynamic sql to insert data in table
hi I wish to insert 5 records in a table at runtime. i.e the data should be entered at runtime. I tried using a for loop but failed .. I think it is possible thru dynamic sql ... could someone help me out .. regards susmitha
-
Best Practice for Change Maintenance Order Number Range
Hello We have decided that our Maintenance Orders will always start with the year of the open date. Example 201112345155 Every year we will need to change the number ranges What is the best practice to change the order number range? What is the best
-
Is it possible to shuffle photos in an iPhoto slideshow project? I created a temporary slideshow with this feature, but need to export it to iDVD. I have photos spanning decades and want them to appear randomly without me having to rearrange them all
-
Some videos not playing in Quicktime but WILL play on VLC Player
Hello, All of a sudden, some of my video files will not play on Quicktime. OT reads the length of the movie but when I click play, all I get is a black screen and no sound even though the progress bar rolls per usual. When I open the videos in my VLC