MS ACCESS or Excel formula or code to result in desired output column -
i have data has 1 many relation. below dummy data represent data. have id have multiple alt_ids. alt_ids have relation column called alt_spec_code. need or desire output column populate let's id_spec_code 1 of alt_spec_codes.
id alt_id alt_spec_code desired_outcome(id_spec_code) 123456 111111 pa pa 123456 222222 n/a pa 123456 121212 n/a pa 654321 333333 n/a st 654321 444444 st st 654321 434343 n/a st 987654 222222 n/a n/a 987654 121212 n/a n/a 987654 333333 n/a n/a 456789 111111 pa both 456789 444444 st both 456789 555555 n/a both
i thinking update statement update desired_outcome field max or maybe doa row_number type query rank alt_spec_code , update desired_outcome field first value finds. wasn't sure how handle last scenario might both.
select y2.id, y2.alt_id, y2.alt_spec_code, switch ( countofalt_spec_code null, 'n/a', sub2.countofalt_spec_code = 2, 'both', sub2.countofalt_spec_code = 1, sub2.minofalt_spec_code ) desired_outcome yourtable y2 left join ( select sub1.id, count(sub1.alt_spec_code) countofalt_spec_code, min(sub1.alt_spec_code) minofalt_spec_code ( select distinct y1.id, y1.alt_spec_code yourtable y1 y1.alt_spec_code<>'n/a' ) sub1 group sub1.id ) sub2 on y2.id = sub2.id;
i didn't know how explain coherently. can figure out how works breaking out each subquery new access query, starting lowest level, select distinct ...
but strategy compute countofalt_spec_code , minofalt_spec_code each id. top-level switch
expression can use information compute desired_outcome.
Comments
Post a Comment