sql server - Update column based on other column values -



sql server - Update column based on other column values -

i have table stores clients grouped under 1 client considered 'head' of group. client considered 'head' if appear in 'group' column, though may not in own 'group'. table may appear follows:

+--------+-------+------+ | client | grouping | head | +--------+-------+------+ | abc | abc | yes | | def | abc | no | | ghi | ghi | yes | | jkl | mno | yes | | mno | pqr | yes | | pqr | mno | no | | stu | stu | yes | +--------+-------+------+

here can see 'head' records client jkl , pqr incorrect. need list of clients 'head' column wrong , should (yes/1 or no/0). best way go doing this?

declare @a table (client varchar(10),[group] varchar(10),head varchar(3)) insert @a values ('abc','abc','yes') insert @a values ('def','abc','no') insert @a values ('ghi','ghi','yes') insert @a values ('jkl','mno','yes') insert @a values ('mno','pqr','yes') insert @a values ('pqr','mno','no') insert @a values ('stu','stu','yes') -- uncomment here wrong ones --select * ( select a.*, coalesce(h.realhead,'no') realhead , case when coalesce(h.realhead,'no')<>a.head 'error' else 'ok' end info @a left bring together (select distinct [group], 'yes' realhead @a) h on h.[group]=a.client -- bring together real heads clients -- uncomment here wrong ones --) s info='error'

sql-server

Comments

Popular posts from this blog

javascript - THREE.js reposition vertices for RingGeometry -

javascript - I need to update the text of a paragraph by inline edit -

assembly - What is the addressing mode for ld, add, and rjmp instructions? -