I have a table with the fields ID,department_code,sub_department_code,class_code,desc_text
The desc_text has the name of the department when the sub_department_code and class_code fields are null, the sub_department name when the department_code and sub_department_code are not null but class_code is null, and the name of the class when the class_code is not null.
My issue is I have another table that has the id from the table above, but I would like to select the fields with department_code,desc_text(of the department),sub_department_code,desc_text(of the sub_department), etc.
I tried a few methods, but I am unsure even how to search for this issue.
I have the following two tables:
Hierarchy
id,department_code,subdept_code,class_code
1 1 null null
2 1 1 null
3 1 1 1
4 2 1 null
Hierarchy_detail
id,hierarchy_id,short_text
1 1 car
2 2 truck
The hierarchy.id is linked to hierarchy_detail.hierarchy_id
With this, I have a table that has data I want, which the fk from hierarchy.id, which I cna link and get the department, sub-sept and class codes, but I am having trouble getting the text on there for each type.
You want to use a case statement to capture the logic. If Im’ reading correctly, it looks something like this:
You can then use this query as a subquery to join to other tables.