Need help in sql update table
Hi,
I have a data in the table like below
Before:
ID SEQ_ID Manager_id Reg_days
1 1 111 1
1 2 222 3
1 3 333 5
1 4 444 7
1 5 555 9
Action: User deletes SEQ_ID #3
After:
ID SEQ_ID Manager_id Reg_days
1 1 111 1
1 2 222 3
1 3 444 7
1 4 555 9
could you please help me the query to update the table how what was seq_id #4 becomes seq_id #3 and what was seq_id #5 becomes seq_id #4; i.e. every sequence number > the one deleted is decremented by one so that the sequence numbers remain contiguous.
Thx in advance
[Edit: why do you never mark any of your questions as "answered"? Providing feedback is a courtesy that encourages people to answer.]
user586 wrote:
Is it possible to update it to write in a procedure or trigger to call the procedure in the code. Plz. let me know or help me.I thought you said you wanted to do this every 3 or 6 months? In that case no trigger!
Do not try to do this in a trigger!
Test data:drop table T;
create table T(id, SEQ_ID, MANAGER_ID, REG_DAYS) as
with IDS as (
select level id from DUAL connect by level <= 10
), REST as (
select level seq_ID, level+100 MANAGER_ID, level*2 - 1 REG_DAYS
from DUAL
connect by level <= 10
select * from IDS, REST;
delete from T where SEQ_ID = 3;
commit;
select * from T where id = 1;
ID SEQ_ID MANAGER_ID REG_DAYS
1 1 101 1
1 2 102 3
1 4 104 7
1 5 105 9
1 6 106 11
1 7 107 13
1 8 108 15
1 9 109 17
1 10 110 19Solution:merge into T
using (
select new_seq_id, rid from (
select SEQ_ID,
ROW_NUMBER() over(partition by id order by SEQ_ID) NEW_SEQ_ID,
rowid RID
from T
where NEW_SEQ_ID != SEQ_ID
) NEW_T
on (T.rowid = NEW_T.RID)
when matched then update set seq_id = new_t.new_seq_id;
70 rows merged.
select * from T where id = 1;
ID SEQ_ID MANAGER_ID REG_DAYS
1 1 101 1
1 2 102 3
1 3 104 7
1 4 105 9
1 5 106 11
1 6 107 13
1 7 108 15
1 8 109 17
1 9 110 19Most importantly, if you run the MERGE twice in a row, the second time you get "0 rows merged." The code only changes the rows that need to be changed.
Edited by: Stew Ashton on Oct 29, 2012 9:15 AM
Similar Messages
-
Need help with SQL Query with Inline View + Group by
Hello Gurus,
I would really appreciate your time and effort regarding this query. I have the following data set.
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*20.00*-------------19
1234567----------11223--------------7/5/2008-----------Adjustment for bad quality---------44345563------------------A-----------------10.00------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765--------------------I---------------------30.00-------------19
Please Ignore '----', added it for clarity
I am trying to write a query to aggregate paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number. When there are no multiple records I want to display the respective Description.
The query should return the following data set
Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*10.00*------------19
7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765-------------------I---------------------30.00--------------19
The following is my query. I am kind of lost.
select B.Description, A.sequence_id,A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
from (
select sequence_id,check_date, check_number, invoice_number, sum(paid_amount) amount, vendor_number
from INVOICE
group by sequence_id,check_date, check_number, invoice_number, vendor_number
) A, INVOICE B
where A.sequence_id = B.sequence_id
Thanks,
NickIt looks like it is a duplicate thread - correct me if i'm wrong in this case ->
Need help with SQL Query with Inline View + Group by
Regards.
Satyaki De. -
I need help! I updated the new 6.1.3 last night. Since that time my phone will not charge. The phone is plugged in and shows as charging but the battery still drains. What can I do? I need help!
Restore iPhone with iTunes on your computer.
-
new update on my iTunes 8.2 computer. Sync my iPads on the computer missing playlist on my computer ??? Need help. Worst update ever
Hello there, dancingteacher.
Removing and re-installing iTunes can be a challenging endeavor and has to be done in a very particular way. The following Knowledge Base article provides a clear, step-by-step process of removing iTunes:
Issues installing iTunes or QuickTime for Windows
http://support.apple.com/kb/ht1926
The "Additional Troubleshooting" section also includes a link to updates and conflicts with software for your Windows Operating System as well.
Thanks for reaching out to Apple Support Communities.
Cheers,
Pedro. -
Need help in SQL Query: Update a row in a table & insert the same row into another table
I want to update a row in a table say Table A and the updated row should be inserted into another table say Table B. I need to do it in a single SQL query and i don't want to do it in PL/SQL with triggers. And i tried with MERGE statement but its working with this scenario. (Note: I'm using Oracle Database 10g Enterprise Edition Release 10.2.0.1.0).
Thanks in Advance.Using Sven's code as an example, you could save the updated row in a sql plus variable. (also untested):
SQL> var v_id number
update tableA
set colB='ABC'
where colC='XYZ'
returning id into :v_id;
insert into table A_History (ID, colA, colB, ColC)
select id, ColA, ColB, ColC
from tableA
where id = :v_id; -
Need help in SQL (DENSE_RANK) function
Hello All,
I need the help in SQL.
We have a table called status and the column are
status_id number
account_id number
status_cd varchar2(10)
created_id varchar2(10)
created_by date
and data is as follows
insert into status values (1,101,'ENTER','ABC',to_date('21-JAN-2007 11:15:14','DD-MON-YYYY HH:MI:SS'));
insert into status values (2,101,'REVIEW','DEF',to_date('21-JAN-2007 11:30:25','DD-MON-YYYY HH:MI:SS'));
insert into status values (3,101,'APPROVE','GHI',to_date('21-JAN-2007 11:30:25','DD-MON-YYYY HH:MI:SS'));
insert into status values (4,102,'ENTER','ABC',to_date('21-JAN-2007 11:18:14','DD-MON-YYYY HH:MI:SS'));
insert into status values (5,102,'REVIEW','DEF',to_date('21-JAN-2007 11:33:25','DD-MON-YYYY HH:MI:SS'));
insert into status values (6,102,'CANCEL','GHI',to_date('21-JAN-2007 11:33:25','DD-MON-YYYY HH:MI:SS'));
insert into status values (7,103,'ENTER','ABC',to_date('21-JAN-2007 11:21:14','DD-MON-YYYY HH:MI:SS'));We have different status as follows
1. ENTER
2. REVIEW
3. APPROVE
4. CANCEL
5. REJECT
My requirement ..
I need the max of created_id column for the status in ('APPROVE','CANCEL') and if there is no status in ('APPROVE','REVIEW') than it should be NULL.
I wrote an SQL as
select account_id,max(created_id) keep (dense_rank first order by decode(status_cd,'APPROVE',created_dt,'REVIEW',created_dt,NULL) DESC NULLS LAST,
decode(status_cd,'APPROVE',status_id,'REVIEW',status_id,NULL) DESC NULLS LAST) last_app_rev_user
from status
group by account_id and gives me the output like
ACCOUNT_ID LAST_APP_R
101 GHI
102 DEF
103 ABCBut I want the Output like
ACCOUNT_ID LAST_APP_R
101 GHI
102 DEF
103 NULLAs the account 103 has no status called 'REVIEW' and 'APPROVE'
My DB Version in 10.2.0.3.0.
Hope I explain it properly. And if you have any other option without dense_rank still i will be happy.
Thanks in advance for your help.
AB
null
Message was edited by:
ABinstead of max(created_id) keep... use
smth like max(case when status_cd in ('APPROVE','REVIEW') then created_id end) keep... -
Need help in creating multivalued table
hi all,
please i need help how to create a table with a column that require to have many cells in each cell of each record.
plz help me with the datatype and the sql syntax.
thanksyou mean nested tables?
Examples are here
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_7002.htm#i2139603
Sybrand Bakker
Senior Oracle DBA -
Hi,
Need help to write sql statement.
create table t_dt ( dt_start date, dt_end date, amount number);
insert into t_dt values('1-Jan-10','10-Feb-10',12);
insert into t_dt values('11-Feb-10','10-Mar-10',10);
insert into t_dt values('11-Mar-10','20-Apr-10',8);
insert into t_dt values('21-Apr-10','28-Jun-10',10);
insert into t_dt values('29-Jun-10','20-Sep-10',10);
insert into t_dt values('21-Sep-10','10-Oct-10',10);
insert into t_dt values('11-Oct-10','31-Dec-10',8);
insert into t_dt values('1-Jan-11','10-Feb-11',8);
insert into t_dt values('11-Feb-11','10-Mar-11',7);
insert into t_dt values('11-Mar-11','20-Apr-11',6);
insert into t_dt values('21-Apr-11','28-Jun-11',6);
insert into t_dt values('29-Jun-11','20-Sep-11',6);
insert into t_dt values('21-Sep-11','10-Oct-11',4);
insert into t_dt values('11-Oct-11','31-Dec-11',8);
Result should be like below..
dt_start dt_end Amount
1-Jan-10 10-Feb-10 12
11-Feb-10 10-Mar-10 10
11-Mar-10 20-Apr-10 8
21-Apr-10 10-Oct-10 10
11-Oct-10 10-Feb-11 8
11-Feb-11 10-Mar-11 7
11-Mar-11 20-Sep-11 6
21-Sep-11 10-Oct-11 4
11-Oct-11 31-Dec-11 8
Just to explain the example, take a row with start date as 21-Apr-10 in the above insert statements, since it has the same amount for next two rows (i.e. with start date '29-Jun-10' and '21-Sep-10') these 3 rows should be converted to represent only 1 row in the result and the start date and end date should be changed per the result shown above.
Thanks.Hello
I think this gives yuo what you need....
SELECT
MIN(dt_start),
MAX(dt_end),
amount
FROM
( SELECT
dt_start,
dt_end,
MAX(marker) OVER(ORDER BY dt_start) marker,
amount
FROM
Select
dt_start,
dt_end,
amount,
CASE
WHEN LAG(amount) OVER(ORDER BY dt_start) <> amount THEN
ROW_NUMBER() OVER(ORDER BY dt_start)
END marker
from t_dt
GROUP BY
amount,
marker
order by
MIN(dt_start)
MIN(DT_START) MAX(DT_END) AMOUNT
01-JAN-2010 00:00:00 10-FEB-2010 00:00:00 12
11-FEB-2010 00:00:00 10-MAR-2010 00:00:00 10
11-MAR-2010 00:00:00 20-APR-2010 00:00:00 8
21-APR-2010 00:00:00 10-OCT-2010 00:00:00 10
11-OCT-2010 00:00:00 10-FEB-2011 00:00:00 8
11-FEB-2011 00:00:00 10-MAR-2011 00:00:00 7
11-MAR-2011 00:00:00 20-SEP-2011 00:00:00 6
21-SEP-2011 00:00:00 10-OCT-2011 00:00:00 4
11-OCT-2011 00:00:00 31-DEC-2011 00:00:00 8
9 rows selected.HTH
David
Edited by: Bravid on Feb 23, 2012 12:08 PM
Beaten to it by Frank! :-) -
Need a Query to update table from another table.
I have two tables Table A and Table B , till now in table “A”. I have a column which consist of IDs and duplicate IDs( which are basically formed by Original Ids) , I have another table i.e. table “B” , which gives a mapping between original ids and duplicate Ids
TABLE A:
ID/DUPLICATEID NAME
1 Rahul
1_CAD Pawan
2 Nikhil
3 TOM
3_CAD Ravi
3_MQ Puneet
TABLE B:
ORIGINALID DUPLICATEID
1 1_CAD
3 3_CAD
3 3_MQ
Now I want to have another column in Table “A” , which will give me the mapping between the original Id and duplicate Id as shown in updated table “A”.
UPDATED TABLE A:
ID/DUPLICATEID NAME ORIGINAL_ID
1 Rahul
1_CAD Pawan
2 Nikhil
3 TOM
3_CAD Ravi
3_MQ Puneet
Now I want to write a Query in which I can update this column (ORIGINAL_ID) of Table “A”, from the table B(basically want to update mulitple rows using single query), because table B already has this mapping. Can any one help me in this. I am basically a Java guy , so I don’t know much about it. I hope to get a positive response from you people, Thanks in advance!Here you go...
<pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%">
<code>
SQL> CREATE TABLE A (ID VARCHAR2(10), NAME VARCHAR2(10), NEW_ID VARCHAR2(10));
Table created.
SQL> INSERT INTO A VALUES ('1', 'Rahul', '');
1 row created.
SQL> INSERT INTO A VALUES ('1_CAD', 'Pawan', '');
1 row created.
SQL> INSERT INTO A VALUES ('2', 'Nikhil', '');
1 row created.
SQL> INSERT INTO A VALUES ('3', 'TOM', '');
1 row created.
SQL> INSERT INTO A VALUES ('3_CAD', 'Ravi', '');
1 row created.
SQL> INSERT INTO A VALUES ('3_MQ', 'Puneet', '');
1 row created.
SQL> CREATE TABLE B (ID VARCHAR2(10), NAME VARCHAR2(10));
Table created.
SQL> INSERT INTO B VALUES ('1', '1_CAD');
1 row created.
SQL> INSERT INTO B VALUES ('3', '3_CAD');
1 row created.
SQL> INSERT INTO B VALUES ('3', '3_MQ');
1 row created.
SQL> COMMIT;
Commit complete.
SQL> UPDATE A
2 SET NEW_ID = NVL((SELECT B.ID FROM B WHERE A.ID = B.NAME),A.ID)
3 /
6 rows updated.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM A;
ID NAME NEW_ID
1 Rahul 1
1_CAD Pawan 1
2 Nikhil 2
3 TOM 3
3_CAD Ravi 3
3_MQ Puneet 3
6 rows selected.
</code></pre>
Note: While asking question do give us DML/DDL script. It will make peoples life better.
Karthick.
http://www.karthickarp.blogspot.com/ -
Regex help for SQL update statement
Hello,
need help from IPS regex guru - trying to build the signature to detect SQL update statement in HTTP requests.
1) Am I correct with regex below specified as Request-Regex?
[Uu][Pp][Dd][Aa][Tt][Ee]([%]20|[+])[\x20-\x7e]+[Ss][Ee][Tt]([%]20|[+])[\x20-\x7e]+=
2) How do I make sure that it detects 'Update' in URI and Arguments only and not in the body on entire webserver response (currently looks like the case)?1) It looks correct to me
2) Typically, the "service HTTP" engine is used to inspect requests and the "TCP string" engine is used to inspect HTTP server responses. If you only want to inspect requests, use the service HTTP engine. -
NEED HELP IN SQL HOMEWORK PROBLEMS
I NEED HELP IN MY SQL HOMEWORK PROBLEMS....
I CAN SEND IT VIA EMAIL ATTACHMENT IN MSWORD....Try this:
SELECT SUBSTR( TN,
DECODE(LEVEL, 1, 1, INSTR(TN, '#', 1, LEVEL-1) + 1),
DECODE( INSTR(TN, '#', 1, LEVEL) , 0 ,
LENGTH(TN) + 1, INSTR(TN, '#', 1, LEVEL) )
- DECODE(LEVEL, 1, 1, INSTR(TN, '#', 1, LEVEL-1 ) + 1)
) xxx
FROM (
SELECT '234123#1254343#909823#908232#12345' TN FROM DUAL
CONNECT BY LEVEL <= LENGTH(TN) - LENGTH(REPLACE(TN,'#')) + 1
XXX
234123
1254343
909823
908232
12345
SELECT regexp_substr(tn, '[^#]+', 1, level) xx
FROM (
SELECT '234123#1254343#909823#908232#12345' TN FROM DUAL
CONNECT BY LEVEL <= LENGTH(TN) - LENGTH(REPLACE(TN,'#')) + 1
XX
234123
1254343
909823
908232
12345 -
Need help in Creating External Tables
Hi All,
I have a flat file containing numberic data, CLOB data, and also date columns data. I have to load this flat file data into staging server table using External tables. I have to write a stored procedure in such way creating exteranl tables dynamically. My question or need help from you people is that how to define the external table to load the CLOB data from flat file.
Thanks,
SankarThe LOCATION clause of an external table specifies the file or files to be read when the table is queried. You can change it to refer to different files without dropping and recreating the table and invalidating all dependent code.
Regarding DBMS_SQL, yes you can do it the hard way if you prefer.
Good point about CLOB columns in external tables. Quite possibly they are not supported, but I would have to check the manuals and try some examples. -
Need help pl/sql function body returning SQL query - Reports
I need help with Grouping by on a report that I developed in my application.
I have posted my Code in
Apex.oracle.com
workspace : c a s e _ m a n a g e m e n t
User Id : public
Password : public
I need help on Page 38 Reports.
I get blank lines when I do break by first , second and third columns on the reports attribute.
So I think I have to write "group by " or Distinct in side the SQL query. But not sure how to do it there.
Thank youIs this an APEX question, then try here:
Oracle Application Express (APEX)
Is this an Oracle Reports question, then try here:
Reports
Is this an SQL question:
Please provide sample data and expected output. Also please show what you have tried already. -
I am using Sybase as my back end database. I need help on my SQL statement regarding datetime. The datetime is store as a 9 digit integer in the database (...I believe it is a Decimal(30,6) format, let me know if I am wrong).
If I do this, "select * from mytable;" It works out fine (except I don't know what the date means e.g. 998919534)
If I do this, "select * from mytable where datetime_col < '9/5/2002' ; I got an error. I even tried '9/5/02 11:00 000 am' but it didn't help.
How do I specify a date or datetime in my query?
Thanks in advance.I execute the sql statement
select * from mytable where datetim_col < convert(datetime, '3/4/2002', 101) ;
I got mix result. I got an error message "cannot convert 10375584 to a date.
Yet, he statistics window of the I-sql says
"estimated 493 rows in query (I/O estimate 87)
PLan> mytable (seq)"
It looks like I my the SQL statement is correct and then the system can't display it in the Data window.
Any thought ? -
Need help on the update statement
Hi,
I have a small requirement. I have a table called test1 and it has 2 columns, nam and seq. nam is a VARCHAR2(100) and seq is a NUMBER.
In the seq I have values like, 2, 3, 7, 9, 25. I would like to update the seq value in an order such that it will be modifed like, 1, 2, 3, 4 and 5.
Here is the scripts, I have used.
CREATE TABLE test1
(nam VARCHAR2(100),
seq NUMBER);
INSERT INTO test1 VALUES('AAA', 2);
INSERT INTO test1 VALUES('BBB', 3);
INSERT INTO test1 VALUES('CCC', 7);
INSERT INTO test1 VALUES('DDD', 9);
INSERT INTO test1 VALUES('EEE', 25);
COMMIT;
And, I would like to achive this in a simple single DML statement. (The required output would be something like, AAA - 1, BBB - 2 etc,...)
I have tried the following query. It is working fine. But If I have a better solution to it, I would to implement the same.
UPDATE test1 a
SET seq =
(SELECT rn
FROM (SELECT ROW_NUMBER() OVER(ORDER BY seq) rn FROM test1) x
WHERE a.rowid = x.rowid);
PS: I am using Oracle 11.2.0.2.0 (11g)
Thanks In Advance!
With Regards,
VSTSQL> CREATE TABLE test1
2 (nam VARCHAR2(100),
3 seq NUMBER);
Table created.
SQL> INSERT INTO test1 VALUES('AAA', 2);
1 row created.
SQL> INSERT INTO test1 VALUES('BBB', 3);
1 row created.
SQL> INSERT INTO test1 VALUES('CCC', 7);
1 row created.
SQL> INSERT INTO test1 VALUES('DDD', 9);
1 row created.
SQL> INSERT INTO test1 VALUES('EEE', 25);
1 row created.
SQL> COMMIT;
Commit complete.
SQL> select * from test1;
NAM SEQ
AAA 2
BBB 3
CCC 7
DDD 9
EEE 25
5 rows selected.
SQL> update test1 set seq = rownum;
5 rows updated.
SQL> select * from test1;
NAM SEQ
AAA 1
BBB 2
CCC 3
DDD 4
EEE 5
5 rows selected.
Maybe you are looking for
-
ANT task wsgen error while deploying RPC style web service
Hi I am trying to assemble/deploy an rpc style weblogic web service using the 'wsgen' Ant task and the build.xml scripts provided with the samples. I get this error: Please help Thanks, Praveen C:\test>ant Buildfile: build.xml webservice: clean: [del
-
How to install adobe on safari
How to install adobe on safari
-
How to implement this functionality?
hi, I want to use a thread to send all message centrally. The thread will check if the internal queue is empty. If the queue isn't empty, it sends the message, otherwise, it yields. The code snippet is as the following. public void run() logger
-
Saving With An Earlier Version
Trying to save a numbers file so I can send to my daughter who has an earlier version of Numbers. Anyone know how?
-
Cisco VPN client -install error
Hi, I have a problem when i'm trying to install Cisco VPN client on my Windows 8.1 x64. After downloading 3 or 4 different versions of Cisco it keeps rising the same error concerning a DLL that is required. I'd really appreciate some advice or the re