sql - update all duplicate rows with different values in pgsql -


i need update duplicate rows different values in same table. table is

table(id, phoneid(int), deviceid(int), userid(int)) 

there records same deviceid or phoneid. example

id   phoneid   deviceid    userid 1    23        3434        1235 2    23        5453        235   <---- same phoneid 1 record  3    43        5453        2343  <---- same deviceid 2 record 4    23        3434        6347  <---- same deviceid , phoneid 1 record 

what need change - if phoneid not unique, set phoneid userid(from row). same @ deviceid. (if deviceid not unique, set deviceid userid) final result should this

id   phoneid   deviceid    userid 1    23        3434        1235 2    235       5453        235   <---- phoneid changed userid 3    43        2343        2343  <---- phoneid changed userid 4    6347      6347        6347  <---- phoneid , deviceid changed userid 

just update duplicated phoneids , duplicated deviceids (assuming table name "t")

update t set phoneid=userid (select count(*),phoneid t group phoneid having count(*)>1) foo t.phoneid=foo.phoneid; update t set deviceid=userid (select count(*),deviceid t group deviceid having count(*)>1) foo t.deviceid=foo.deviceid;

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -