susieqtotallynew
MIS
I am working with a MS SQL database.
The database stores various names for constituents. Each constituent can have multiple name types. Each name type is assigned an ID number. For example, I might have an individual name type, assigned name_type_ID 1. I may also have a joint name type, assigned name_type_ID 2. My partner might have a name type, as well, assigned name_type_3.
The table representation would look like this:
ID, Name, Name_Type_ID
123, Ms. Susie Sample, 1
123, Mr. and Mrs. John Sample, 2
123, Mr. John Sample, 3
333, Mr. Andrew Jones, 1
Note that not every constituent will have every name type.
I need to be able to return a single name type for each ID number based on a priority listing. For example, if name type 2 exists for a given ID, I would return that item. If name type 2 does not exist, I would take name type 1 instead.
Does anyone have advice about a SQL statement that would accomplish this?
Thank you in advance.
The database stores various names for constituents. Each constituent can have multiple name types. Each name type is assigned an ID number. For example, I might have an individual name type, assigned name_type_ID 1. I may also have a joint name type, assigned name_type_ID 2. My partner might have a name type, as well, assigned name_type_3.
The table representation would look like this:
ID, Name, Name_Type_ID
123, Ms. Susie Sample, 1
123, Mr. and Mrs. John Sample, 2
123, Mr. John Sample, 3
333, Mr. Andrew Jones, 1
Note that not every constituent will have every name type.
I need to be able to return a single name type for each ID number based on a priority listing. For example, if name type 2 exists for a given ID, I would return that item. If name type 2 does not exist, I would take name type 1 instead.
Does anyone have advice about a SQL statement that would accomplish this?
Thank you in advance.