Postgres数据库organization表:
CREATE TABLE ORGANIZATION( -- 分公司与部门表
DWBH INTEGER NOT NULL, -- 部门ID
DWMC VARCHAR(50) NOT NULL, --部门名称
FGS INTEGER NOT NULL, --分公司ID
FGSMC VARCHAR(50) NOT NULL --分公司名称
)
优化前:
select concat(fgsmc,'---',dwmc) as dwmc from organization where fgsmc!=dwmc
union all select distinct dwmc from organization where fgsmc=dwmc
优化后:
select case when fgsmc!=dwmc then concat(fgsmc,'---',dwmc)
when fgsmc=dwmc then dwmc end as dwmc from organization