CREATE PROCEDURE P_regOwnerShipStatis(@district nvarchar(50))
AS
BEGINselect * into #temp from (
select 'suoyouquan' statisName,ship_district,COUNT(*) tongji ,SUM(ship_tot_ton) tot_ton,SUM(ship_tot_power)tot_power from reg_ownership_app where status=2 and cert_type=1 and app_type in(1,2,3,4)
group by ship_district
union all
select 'guoji' statisName,ship_district,COUNT(*)tongji ,SUM(ship_tot_ton) tot_ton,SUM(ship_tot_power)tot_power from reg_ownership_app where status=2 and cert_type=3 and app_type in(1,2,3,4)
group by ship_district
union all
select 'diyaquan' statisName,ship_district,COUNT(*)tongji ,SUM(ship_tot_ton) tot_ton,SUM(ship_tot_power)tot_power from reg_hypothec_app where status=2  
group by ship_district
union all
select 'guangchuanzuling' statisName,send_district_id,COUNT(*)tongji ,SUM(ship_tot_ton) tot_ton,SUM(ship_tot_power)tot_power from reg_leasehold_app a where status=2  
group by send_district_id
) as a
select ship_district
,(select d.districtName from taw_district d where d.districtId=ship_district and d.deleted=0) districtName
,sum(case when statisName='suoyouquan' then tongji else 0 end) 所有权船数
,sum(case when statisName='suoyouquan' then tot_ton else 0 end) 所有权吨位
,sum(case when statisName='suoyouquan' then tot_power else 0 end) 所有权功率
,sum(case when statisName='guoji' then tongji else 0 end) 国籍船数
,sum(case when statisName='guoji' then tot_ton else 0 end) 国籍吨位
,sum(case when statisName='guoji' then tot_power else 0 end) 国籍功率
,sum(case when statisName='diyaquan' then tongji else 0 end) 抵押权船数
,sum(case when statisName='diyaquan' then tot_ton else 0 end) 抵押权吨位
,sum(case when statisName='diyaquan' then tot_power else 0 end) 抵押权功率
,sum(case when statisName='guangchuanzuling' then tongji else 0 end) 租赁船数
,sum(case when statisName='guangchuanzuling' then tot_ton else 0 end) 租赁吨位
,sum(case when statisName='guangchuanzuling' then tot_power else 0 end) 租赁功率
into #statis
from #temp where ship_district in(
select districtId from dbo.F_getChildDistrict('370400')
)group by ship_district order by ship_districtselect * from #statis
END
GO