2017-02-08 21 views
0

クエリを実行してその結果をテンポラリテーブルに保存した後、テンポラリテーブルを使用したい。私の手続きは以下の通りですストアドプロシージャmysqlで複数の動的クエリを実行する

CREATE DEFINER=`be4`@`%` PROCEDURE `Report_filter_Avg`(in _year nvarchar(500),in _month nvarchar(500), in _region nvarchar(500), 
in _team nvarchar(500),in _project nvarchar(50000),in _netmr nvarchar(10),in _proposal nvarchar(10),in _maconomy nvarchar(500)) 
BEGIN 
if(_year!='') then begin set @year_=concat(' and ',_year); end; else set @year_=''; end if; 
if(_month!='') then begin set @month_=concat(' and ',_month); end; else set @month_=''; end if; 
if(_region!='') then begin set @region_=concat(' and ',_region); end; else set @region_=''; end if; 
if(_team!='') then begin set @team_=concat(' and ',_team); end; else set @team_=''; end if; 
if(_project!='') then begin set @project_=concat(' and ',_project); end; else set @project_=''; end if; 
if(_netmr!='') then begin set @netmr_=concat(' and netmr_sn=\'',_netmr,'\''); end;else set @netmr_=''; end if; 
if(_proposal!='') then begin set @proposal_=concat(' and Proposal_sn=\'',_proposal,'\''); end; else set @proposal_=''; end if; 
if(_maconomy!='') then begin set @maconomy_=concat(' and Maconomy_no like \'',_maconomy,'%\''); end; else set @maconomy_=''; end if; 
set @query_T=concat('create temporary table tbl engine=memory select * from(
select year, month,Team_Name,sum(studies)Studies, sum(SO)Hrs from production_report where SO > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''), 
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name union all 
select year, month,concat(Region_Name,''_SP'')Team_Name,sum(studies)Studies, sum(SP)Hrs from production_report where SP > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''), 
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name union all 
select year, month,concat(Region_Name,''_DP'')Team_Name,sum(studies)Studies, sum(DP)Hrs from production_report where DP > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''), 
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name union all 
select year, month,concat(Region_Name,''_CS'')Team_Name,sum(studies)Studies, sum(CS)Hrs from production_report where CS > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''), 
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name union all 
select year, month,Region_Name,sum(studies)Studies, sum(TAB)Hrs from production_report where TAB > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''), 
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name union all 
select year, month,''OE'',sum(studies)Studies, sum(OE)Hrs from production_report where OE > 0 ',coalesce(@year_,''),coalesce(@month_,''),coalesce(@region_,''), 
coalesce(@team_,''),coalesce(@project_,''),coalesce(@netmr_,''),coalesce(@proposal_,''),coalesce(@maconomy_,''),' group by Year,Month,Team_Name 
)B;'); 
PREPARE st FROM @query_T; 
EXECUTE st; 
DEALLOCATE PREPARE st; 
set @query_C=('create temporary table tbl_count(select Year,Month,Team_Name,count(Month)count from tbl group by Year,Team_Name);'); 
PREPARE st FROM @query_C; 
EXECUTE st; 
DEALLOCATE PREPARE st; 
set @query_M=('select B.Year,concat(B.Team_Name,''('',B.Year,'')'')Team_Name,sum(Jan)Jan,sum(Feb)Feb,sum(Mar)Mar,sum(Apr)Apr,sum(May)May,sum(Jun)Jun,sum(Jul)Jul, 
sum(Aug)Aug,sum(sep)Sep,sum(Oct)Oct,sum(Nov)Nov,sum(dec_)Dec_,round((sum(Jan)+sum(Feb)+sum(Mar)+sum(Apr)+sum(May)+sum(Jun)+sum(Jul)+sum(Aug) 
+sum(sep)+sum(Oct)+sum(Nov)+sum(Dec_))/count,1) Total from(
SELECT Year,Month,Team_Name,case when Month=1 then Hrs else 0 end Jan,case when Month=2 then Hrs else 0 end Feb, 
case when Month=3 then Hrs else 0 end Mar,case when Month=4 then Hrs else 0 end Apr,case when Month=5 then Hrs else 0 end May, 
case when Month=6 then Hrs else 0 end Jun,case when Month=7 then Hrs else 0 end Jul,case when Month=8 then Hrs else 0 end Aug, 
case when Month=9 then Hrs else 0 end Sep,case when Month=10 then Hrs else 0 end Oct,case when Month=11 then Hrs else 0 end Nov, 
case when Month=12 then Hrs else 0 end Dec_ from(
select Year,Month,Team_Name,round(Hrs/Studies,1)Hrs from tbl group by Team_Name,Month,Year)A group by Year,Month,Team_Name 
)B left join tbl_count on tbl_count.Team_Name=B.Team_Name and tbl_count.Year=B.Year 
group by Year,Team_Name order by Team_Name ASC;'); 
PREPARE st FROM @query_M; 
EXECUTE st; 
DEALLOCATE PREPARE st; 
set @query_D1=('drop temporary table tbl;drop temporary table tbl_count;'); 
PREPARE st FROM @query_D; 
EXECUTE st; 
DEALLOCATE PREPARE st; 
set @query_D2=('drop temporary table tbl_count;'); 
PREPARE st FROM @query_D2; 
EXECUTE st; 
DEALLOCATE PREPARE st; 

上記のコードは正しいですか?これをどうすれば使えますか?

答えて

0

なぜviewを使用しませんか?

SQLでは、ビューはSQLステートメントの結果セットに基づく仮想テーブルです。これは、テーブルを変更する必要がない場合に役立ちます。

ビューには、実際の表のように行と列が含まれています。ビューのフィールドは、データベース内の1つ以上の実表のフィールドです。

SQLファンクション、WHEREおよびJOIN文をビューに追加し、データが単一の表から来ているかのように表示できます。

あなたはw3school

のビューについての詳細を読むことができます
関連する問題