pdf_code/zzb_data_prod/处理历史数据/外部数据导入宽表 -年报半年报.sql

227 lines
5.4 KiB
SQL

set @year = "2024";
SET @next_year = CAST(CAST(@year AS UNSIGNED) + 1 AS CHAR);
set @enddate = CONCAT(@year,"" ,"-6-30 00:00:00");
set @enddate_1 = CONCAT(@year,"" ,"-12-31 00:00:00");
set @InfoPublDate = CONCAT(@next_year,"" ,"-9-30 00:00:00");
-- 年报是0 半年报是1
set @report_type = 1;
drop table if exists third_quarter_external_data_tmp_pre;
create table third_quarter_external_data_tmp_pre
as
select distinct @year as year
,@report_type as report_type
,t1.*
,Infopubldate
,InfoPublDate_end
,OperatingReenue
,NPFromParentCompanyOwners
,NetProfitCut
,NetOperateCashFlow
,NetFinanceCashFlow
,NetInvestCashFlow
,NonRecurringProfitLoss
,BasicEPS
,DilutedEPS
,WROE
,WROECut
,TotalAssets
,CashEquivalents
,AccountReceivable
,Inventories
,TotalFixedAsset
,TConstruInProcess
,GoodWill
,ShortTermLoan
,AccountsPayable
,ContractLiability
,LongtermLoan
,AccountingStandards
,OperatingExpense
,OperatingCost
,AdministrationExpense
,FinancialExpense
,RAndD
from
(
select
InnerCode
,CompanyCode
,LPAD(SecuCode,6,'0') as SecuCode
,ChiName
from secumain s
where ListedState=1
and ListedSector in (1,2,6,7,8)
and SecuCategory in (1,41)
and SecuMarket in (18,83,90)
)t1
left join
(
select
CompanyCode
,Infopubldate
,OperatingReenue
,NPFromParentCompanyOwners
,NetProfitCut
,NetOperateCashFlow
,NetFinanceCashFlow
,NetInvestCashFlow
,NonRecurringProfitLoss
,BasicEPS
,DilutedEPS
,WROE
,WROECut
from
(
SELECT *
FROM (SELECT b.*
,@rownum := @rownum+1
,IF(@pdept = b.CompanyCode, @rank := @rank + 1, @rank := 1) AS rank_num
,@pdept := b.CompanyCode
FROM (SELECT * FROM lc_maindatanew a
where EndDate=@enddate and InfoPublDate < @InfoPublDate
and mark NOT IN (4, 5)
ORDER BY a.CompanyCode, a.InfoPublDate DESC) b
-- 初始化自定义变量值
,(SELECT @rownum :=0, @pdept := NULL, @rank:=0) c
-- 该排序必须,否则结果会不对
ORDER BY b.CompanyCode, b.InfoPublDate DESC) result
order by CompanyCode, rank_num
)t where t.rank_num=1
)t2
on t1.CompanyCode = t2.CompanyCode
left join
(
select
EndDate,
InfoSource,
CompanyCode
from
(
SELECT *
FROM (SELECT b.*
,@rownum := @rownum+1
,IF(@pdept = b.CompanyCode, @rank := @rank + 1, @rank := 1) AS rank_num
,@pdept := b.CompanyCode
FROM (SELECT * FROM lc_maindatanew a
where EndDate=@enddate and InfoPublDate < @InfoPublDate
and mark NOT IN (4, 5)
ORDER BY a.CompanyCode, a.InfoPublDate DESC) b
-- 初始化自定义变量值
,(SELECT @rownum :=0, @pdept := NULL, @rank:=0) c
-- 该排序必须,否则结果会不对
ORDER BY b.CompanyCode, b.InfoPublDate DESC) result
order by CompanyCode, rank_num
)t where t.rank_num=1
)t5
on t1.CompanyCode = t5.CompanyCode
left join
(
select
CompanyCode,
TotalAssets
from
(
SELECT *
FROM (SELECT b.*
,@rownum := @rownum+1
,IF(@pdept = b.CompanyCode, @rank := @rank + 1, @rank := 1) AS rank_num
,@pdept := b.CompanyCode
FROM (SELECT * FROM lc_maindatanew a
where EndDate=@enddate_1 and InfoPublDate < @InfoPublDate
and mark NOT IN (4, 5)
ORDER BY a.CompanyCode, a.InfoPublDate DESC) b
-- 初始化自定义变量值
,(SELECT @rownum :=0, @pdept := NULL, @rank:=0) c
-- 该排序必须,否则结果会不对
ORDER BY b.CompanyCode, b.InfoPublDate DESC) result
order by CompanyCode, rank_num
)t where t.rank_num=1
)t6
on t1.CompanyCode = t6.CompanyCode
left join
(
select
EndDate,
InfoPublDate as InfoPublDate_end,
InfoSource,
CompanyCode,
CashEquivalents,
AccountReceivable,
Inventories,
TotalFixedAsset,
TConstruInProcess,
GoodWill,
ShortTermLoan,
AccountsPayable,
ContractLiability,
LongtermLoan
from
(
SELECT *
FROM (SELECT b.*
,@rownum := @rownum+1
,IF(@pdept = b.CompanyCode, @rank := @rank + 1, @rank := 1) AS rank_num
,@pdept := b.CompanyCode
FROM (SELECT * FROM lc_balancesheetall a
where IfMerged =1
and EndDate=@enddate_1 and InfoPublDate < @InfoPublDate
ORDER BY a.CompanyCode, a.InfoPublDate DESC) b
-- 初始化自定义变量值
,(SELECT @rownum :=0, @pdept := NULL, @rank:=0) c
-- 该排序必须,否则结果会不对
ORDER BY b.CompanyCode, b.InfoPublDate DESC) result
order by CompanyCode, rank_num
)t where t.rank_num=1
)t4
on t1.CompanyCode = t4.CompanyCode
left join
(
select
CompanyCode
,EndDate
,AccountingStandards
,OperatingExpense
,OperatingCost
,AdministrationExpense
,FinancialExpense
,RAndD
from
(
SELECT *
FROM (SELECT b.*
,@rownum := @rownum+1
,IF(@pdept = b.CompanyCode, @rank := @rank + 1, @rank := 1) AS rank_num
,@pdept := b.CompanyCode
FROM (SELECT * FROM LC_IncomeStatementAll a
where IfMerged =1
and EndDate=@enddate and InfoPublDate < @InfoPublDate
ORDER BY a.CompanyCode, a.InfoPublDate DESC) b
-- 初始化自定义变量值
,(SELECT @rownum :=0, @pdept := NULL, @rank:=0) c
-- 该排序必须,否则结果会不对
ORDER BY b.CompanyCode, b.InfoPublDate DESC) result
order by CompanyCode, rank_num
)t where t.rank_num=1
)t3
on t1.CompanyCode = t3.CompanyCode
;
INSERT INTO third_measure_data SELECT * from third_quarter_external_data_tmp_pre;
select * from third_measure_data where year = 2024 and report_type = 1;