0

I have 2 tables

aaa
on_date datetime null,
channel_id numeric(10,0) null,
type_id  numeric(10,0) null,
amount numeric(10,0) null

bbb
channel_id  numeric(10,0) null
type_id numeric(10,0) null

I want to do an update to aaa.amount to 0 of

  1. only the common channel_ids and type_ids when bbb.type_id is not null

or

  1. of only the common channel_ids and ALL the type_ids when the bbb.type_id is null

    aaa

    2014-09-13 1 3 12

    2014-09-13 1 4 16

    2014-09-13 2 1 11

    bbb (case 1. )

    1 3

    bbb (case 2. )

    1 null

In first case only the first aaa record should have amount = 0 In the second case the first 2 records of aaa should have amount = 0 Thank you

1
  • Please provide sample data and desired results. Commented Mar 28, 2016 at 12:40

2 Answers 2

2

Try this:

update a
set a.amount=0
from aaa a inner join bbb b 
on a.channel_id=b.channel_id
where ((a.channel_id=b.channel_id and a.type_id=b.type_id and b.type_id IS NOT NULL)
OR (a.channel_id=b.channel_id and b.type_id IS NULL))
Sign up to request clarification or add additional context in comments.

Comments

1

you need to somthing like following as per i understand your question

UPDATE
    aa
SET
    aa.amount  = 0 
FROM
    aaa aa
    JOIN
    bbb bb ON aa.channel_id  =bb.channel_id      
WHERE
    (aa.type_id = bb.type_id AND bb.type_id is not null ) 
    OR
    (bb.type_id is null) 

Hope it will help to you.

Comments

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.