问题: 把相同的组织 日期,地区的多条数据转换成一行数据,也就是多行转成多列
具体sql如下所示:
select
r.org_id,
r.month_id,
r.area_id,
SUM(case when r.data_item_id='D0302' then r.current_val end) current_vala,
SUM(case when r.data_item_id='D0305' then r.current_val end) current_valb,
SUM(case when r.data_item_id='D0307' then r.current_val end) current_valc,
SUM(case when r.data_item_id='D0309' then r.current_val end) current_vald,
SUM(case when r.data_item_id='D0310' then r.current_val end) current_vale,
max(r.modify_time) modify_time,
max(r.modifier) modifier,
max(r.create_time) create_time,
max(r.creator) creator,
max(r.rec_ver) rec_ver,
s.sorg_sname sorg_name,
m.area_name,
p.is_closed
from bistg.ha_profit_area r
left join bistg.mdm_cty_area m on r.area_id=m.area_id
left join bidm.sys_org s on r.org_id = s.sorg_id
left join bistg.etl_period_status p on r.month_id = p.month_id and p.org_id=r.org_id
where r.org_id in (
select o.sorg_id from bidm.sys_org o where o.sorg_id=:orgId
)
<< and r.month_id = :monthId >>
and ##CONDITIONS##
group by r.org_id,r.month_id,r.area_id,s.sorg_sname,m.area_name, p.is_closed
order by max(r.modify_time) desc
结果如下: