2017-02-08 18 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 문의 결과 집합을 기반으로하는 가상 테이블입니다.

보기에는 실제 테이블과 마찬가지로 행과 열이 포함됩니다. 뷰의 필드는 데이터베이스에있는 하나 이상의 실제 테이블의 필드입니다.

뷰에 SQL 함수, WHERE 및 JOIN 문을 추가하고 데이터가 하나의 단일 테이블에서 온 것처럼 데이터를 표시 할 수 있습니다.

당신은 w3school

에서보기에 대한 자세한 내용을보실 수 있습니다