Thursday, 15 March 2012

sql - Deleting the second item from a search in Oracle -



sql - Deleting the second item from a search in Oracle -

a friend of mine found in test, , puzzled it. not using cursors, nor distinct, neither sub-selects, possible delete sec code '2' in table looks this? (preferably single statement)

code | name -------------- 1 | name1 2 | name2 3 | name3 1 | name1 1 | name1 2 | name2 <----delete field 3 | name3

looking @ result set, might point , "second row", technically there no such "guaranteed" sec row. need able order result set in specific order (usually per requirement) , identify sec row.

so, question this, instead, have definitive answer.

delete sec "code 2" table when info ordered name ascending. (even in case, there question of when there multiple rows code 2 , same name)

--identify row deleted.

select rowid ( select rowid, rank () on (order name asc) rnk --or dense_rank --depending on requirement test_table code = 2 )where rnk = 2

--delete row, using selected rowid.

delete test_table rowid in ( <query above..> };

sql oracle

No comments:

Post a Comment