0

I added a column in the Orders table called EventId and I have to fill the column with the right value for every row.

I want to achieve it with a more complex sql query like below:

UPDATE [dbo].[Orders] o
SET o.EventId = ????
WHERE o.Id IN (SELECT o.id, s.eventid FROM orders o
INNER JOIN orderskudiscounts osd ON o.id = osd.orderid
INNER JOIN skus s ON osd.skuid = s.id GROUP BY o.id, s.eventid)

I not sure how can I write the query successfully... I have more than 2 thousand mapping to do. So I should use a query...

Thank for any help

David Létourneau
  • 1,250
  • 2
  • 19
  • 39
  • 1
    Edit your question and provide sample data and desired results. It would help if you explained what "right value" means. – Gordon Linoff Mar 16 '18 at 18:02

1 Answers1

3

I am guessing that you want an update like this:

UPDATE o
SET o.EventId = s.eventid
FROM orders o INNER JOIN
     orderskudiscounts osd
     ON o.id = osd.orderid INNER JOIN
     skus s
     ON osd.skuid = s.id;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786