4

I am new to SQL and I am no good with more advanced queries and functions.

So, I have this 1 table with sales:

 id   date         seller_name   buyer_name
---- ------------ ------------- ------------
 1    2015-02-02   null          Adrian
 1    2013-05-02   null          John B
 1    2007-11-15   null          Chris F
 2    2014-07-12   null          Jane A
 2    2011-06-05   null          Ted D
 2    2010-08-22   null          Maryanne A
 3    2015-12-02   null          Don P
 3    2012-11-07   null          Chris T
 3    2011-10-02   null          James O

I would like to update the seller_name for each id, by putting the buyer_name from previous sale as seller_name to newer sale date. For example, for on id 1 John B would then be seller in 2015-02-02 and buyer in 2013-05-02. Does that make sense?

P.S. This is the perfect case, the table is big and the ids are not ordered so neat.

1
  • I think what are you looking for are the lead and lag functions. Commented Feb 24, 2016 at 8:15

3 Answers 3

5
merge into your_table a 
using ( select rowid rid,
        lead(buyer_name, 1) over (partition by id order by date desc) seller
        from   your_table 
       ) b
       on (a.rowid = b.rid )
when matched then update set a.seller_name= b.seller;

Explanation : Merge into statement performs different operations based on matched or not matched criterias. Here you have to merge into your table, in the using having the new values that you want to take and also the rowid which will be your matching key. The lead function gets the result from the next n rows depending on what number you specify after the comma. After specifying how many rows to jump you also specify on what part to work, which in your case is partitioned by id and ordered by date so you can get the seller, who was the previous buyer. Hope this clears it up a bit.

Sign up to request clarification or add additional context in comments.

Comments

1

Either of the below query can be used to perform the desire action

merge into sandeep24nov16_2 table1
using(select rowid r, lag(buyer_name) over (partition by id order by "DATE" asc) update_value from sandeep24nov16_2 ) table2
on (table1.rowid=table2.r)
when matched then update set table1.seller_name=table2.update_value;

or

merge into sandeep24nov16_2 table1
using(select rowid r, lead(buyer_name) over (partition by id order by "DATE" desc) update_value from sandeep24nov16_2 ) table2
on (table1.rowid=table2.r)
when matched then update set table1.seller_name=table2.update_value;

Comments

0
select a.*,
       lag(buyer_name, 1) over(partition by id order by sale_date) seller_name
   from <your_table> a;

2 Comments

This is just the data but OP actually needs an update statement
@NullException - Sure, I agree, for which your solution is better. I was trying with lag instead of lead. So it is just another way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.