First, i know that my title stinks. Did not know how to properly phrase my question.
i have a code table that i want to pull out the description. I match on code_name and code. But there is a third column, tenant_cd, that describes the type of tenant. Different tenants can have different descriptions. My BA is using a blank tenant_cd as a catch all. If there is a tenant_cd match, then use that description. But if there is not a tenant code match then use the description with the blank tenant_cd. Right now I have an OR condition in my Left Join. But that is causing duplicates. It is like i want to rank my tenant_cd join condition with matching #1 and if not matching then code
this is what i want for the results
create table codes (column_name varchar(50), code varchar(50), code_desc varchar(50), TENANT_CD varchar(50));
insert into codes values ('cas_sta', 'A1', 'FIS CLOSED', 'FIS_TENANT'), ('cas_sta', 'A1', 'COOP CLOSED', 'COOP_TENANT'), ('cas_sta', 'A1', 'OTHER CLOSED', ''),
('cas_sta', 'A2', 'ALL CLOSED', '')
drop table dashboard1_table
create table dashboard1_table (acct_nbr varchar(50), cas_sta varchar(50), TENANT_CD varchar(50));
insert into dashboard1_table values ('111', NULL, ''),
('222', 'A1','FIS_TENANT'),
('223', 'A1','COOP_TENANT'),
('224', 'A1','TENANT XYZ'),
('333', 'A2','FIS_TENANT'),
('444', 'A2','COOP_TENANT'),
('555', 'A0', '');
select
d.acct_nbr,
d.TENANT_CD,
d.cas_sta,
case when c1.code_desc is NULL then 'UNKNOWN' else c1.code_desc end as CAS_STA_DESC
from dashboard1_table d
left join codes c1 on c1.column_name = 'cas_sta' and c1.code = d.cas_sta and (d.tenant_cd = c1.tenant_cd or c1.tenant_cd = '')