sql - Joining to tables while linking with a crosswalk table -
i trying write query de identify 1 of tables. make distinct ids people, used name, age , sex. in main table, data has been collected years , sex code changed 1 meaning male , 2 meaning female m meaning male , f meaning female. make uniform in distinct individuals table used crosswalk table convert sexcode correct format before placing distinct patients table.
i trying write query match distinct patient ids correct rows main. table. issue sexcode has been changed. know use update statement on main table , changes of 1 , 2 m , f. however, wondering if there way match old new sexcodes not have make update. did not know if there way join main , distinct ids tables in query while using sexcode table convert sexcodes again. below example tables using.
this main table want de identify
---------------------------- | name | age | sex | toy | ---------------------------- | stacy| 30 | 1 | bat | | sue | 21 | 2 | ball | | jim | 25 | 1 | ball | | stacy| 30 | m | ball | | sue | 21 | f | glove | | stacy| 18 | f | glove | ----------------------------
sex code crosswalk table
------------------- | sexold | sexnew | ------------------- | m | m | | f | f | | 1 | m | | 2 | f | -------------------
this table used to populate ids people found distinct in main table
-------------------------- | id | name | age | sex | -------------------------- | 1 | stacy| 30 | m | | 2 | jim | 25 | m | | 3 | stacy| 18 | f | | 4 | sue | 21 | f | --------------------------
this want de identified table like
--------------- | id | toy | --------------- | 1 | bat | | 4 | ball | | 2 | ball | | 1 | ball | | 4 | glove | | 3 | glove | ---------------
select c.id, a.toy maintable left join sexcodecrosswalk b on b.sexold = a.sex left join peopleids c on c.name = a.name , c.age = a.age , c.sex = b.sexnew
here's demonstration works: http://sqlfiddle.com/#!3/a2d26/1
Comments
Post a Comment