[置顶] [易飞]主营业务分析

SQL代码:

USE [ZE]
GO
/****** Object:  StoredProcedure [dbo].[UP_MainBusiness]    Script Date: 04/11/2012 11:01:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--============================================================
--用途:主营业务分析表
--作者:龚德辉
--日期:2012-04-10
--============================================================

Create PROCEDURE [dbo].[UP_MainBusiness]

AS
BEGIN
/*主营业业务收入*/
create table #mainbusiness 
(
   item nvarchar(20)
  ,saletype nvarchar(10) 
  ,Jan  decimal(18,2) DEFAULT (0)
  ,Jan1  decimal(18,2) DEFAULT (0)  
  ,Feb decimal(18,2) DEFAULT (0)
  ,Feb1 decimal(18,2) DEFAULT (0)
  ,Mar decimal(18,2) DEFAULT (0)
  ,Mar1 decimal(18,2) DEFAULT (0)
  ,Apr decimal(18,2) DEFAULT (0)
  ,Apr1 decimal(18,2) DEFAULT (0)
  ,May decimal(18,2) DEFAULT (0)
  ,May1 decimal(18,2) DEFAULT (0)
  ,Jun decimal(18,2) DEFAULT (0)
  ,Jun1 decimal(18,2) DEFAULT (0)
  ,Jul decimal(18,2) DEFAULT (0)
  ,Jul1 decimal(18,2) DEFAULT (0)
  ,Aug decimal(18,2) DEFAULT (0)
  ,Aug1 decimal(18,2) DEFAULT (0)
  ,Sep decimal(18,2) DEFAULT (0)
  ,Sep1 decimal(18,2) DEFAULT (0)
  ,Oct decimal(18,2) DEFAULT (0)
  ,Oct1 decimal(18,2) DEFAULT (0)
  ,Nov decimal(18,2) DEFAULT (0)
  ,Nov1 decimal(18,2) DEFAULT (0)
  ,[Dec] decimal(18,2) DEFAULT (0)
  ,[Dec1] decimal(18,2) DEFAULT (0)
)
/*主营业业务成本*/
create table #mainbusinesscost 
(
   item nvarchar(20)
  ,saletype nvarchar(10)    
  ,Jan  decimal(18,2) DEFAULT (0)
  ,Jan1  decimal(18,2) DEFAULT (0)  
  ,Feb decimal(18,2) DEFAULT (0)
  ,Feb1 decimal(18,2) DEFAULT (0)
  ,Mar decimal(18,2) DEFAULT (0)
  ,Mar1 decimal(18,2) DEFAULT (0)
  ,Apr decimal(18,2) DEFAULT (0)
  ,Apr1 decimal(18,2) DEFAULT (0)
  ,May decimal(18,2) DEFAULT (0)
  ,May1 decimal(18,2) DEFAULT (0)
  ,Jun decimal(18,2) DEFAULT (0)
  ,Jun1 decimal(18,2) DEFAULT (0)
  ,Jul decimal(18,2) DEFAULT (0)
  ,Jul1 decimal(18,2) DEFAULT (0)
  ,Aug decimal(18,2) DEFAULT (0)
  ,Aug1 decimal(18,2) DEFAULT (0)
  ,Sep decimal(18,2) DEFAULT (0)
  ,Sep1 decimal(18,2) DEFAULT (0)
  ,Oct decimal(18,2) DEFAULT (0)
  ,Oct1 decimal(18,2) DEFAULT (0)
  ,Nov decimal(18,2) DEFAULT (0)
  ,Nov1 decimal(18,2) DEFAULT (0)
  ,[Dec] decimal(18,2) DEFAULT (0)
  ,[Dec1] decimal(18,2) DEFAULT (0)
)
/*主营业业务开票数量*/
create table #mainbusinessqty
(
   item nvarchar(20)
  ,saletype nvarchar(10) 
  ,Jan  decimal(18,2) DEFAULT (0)
  ,Jan1  decimal(18,2) DEFAULT (0)  
  ,Feb decimal(18,2) DEFAULT (0)
  ,Feb1 decimal(18,2) DEFAULT (0)
  ,Mar decimal(18,2) DEFAULT (0)
  ,Mar1 decimal(18,2) DEFAULT (0)
  ,Apr decimal(18,2) DEFAULT (0)
  ,Apr1 decimal(18,2) DEFAULT (0)
  ,May decimal(18,2) DEFAULT (0)
  ,May1 decimal(18,2) DEFAULT (0)
  ,Jun decimal(18,2) DEFAULT (0)
  ,Jun1 decimal(18,2) DEFAULT (0)
  ,Jul decimal(18,2) DEFAULT (0)
  ,Jul1 decimal(18,2) DEFAULT (0)
  ,Aug decimal(18,2) DEFAULT (0)
  ,Aug1 decimal(18,2) DEFAULT (0)
  ,Sep decimal(18,2) DEFAULT (0)
  ,Sep1 decimal(18,2) DEFAULT (0)
  ,Oct decimal(18,2) DEFAULT (0)
  ,Oct1 decimal(18,2) DEFAULT (0)
  ,Nov decimal(18,2) DEFAULT (0)
  ,Nov1 decimal(18,2) DEFAULT (0)
  ,[Dec] decimal(18,2) DEFAULT (0)
  ,[Dec1] decimal(18,2) DEFAULT (0)
)
--今年与去年年份
declare
@year as nchar(4),
@lastyear as nchar(4),
@cyear as nchar(4),
@cmonth as nchar(3),
@ctype as nvarchar(20),
@csaletype as nvarchar(10),
@cmount as decimal(10,2),
@ccost as decimal(10,2),
@cqty as decimal(10,2)
set @year=datepart(year,getdate()) 
set @lastyear=datepart(year,dateadd(year,-1,getdate()))  

DECLARE MAINBUSINESS CURSOR FOR 
select A.cyear,A.cmonth,A.csaletype,A.ctype,sum(camount),sum(ccost),sum(qty) from( 
SELECT   datepart(year,TA003) cyear,datepart(month,TA003) cmonth,
CASE WHEN TA001 in ('6101','6102','6103','6104','6105','6106')  then '内销' else '外销' END AS csaletype,
case when  A1.MA003 in ('ACM','COG','TAB','AGM','OLED') THEN 'LCM' 
     when  A1.MA003 in ('内制背光','贸易背光') THEN 'BL' 
     when  A1.MA003 ='贸易LCD' THEN 'LCD' 
     when  A1.MA003 ='继电器' THEN 'RELAY' 
     when  A1.MA003 ='TFT' THEN 'VAP' 
     else  'Others'  End as ctype,

TB019 camount, TA001,
convert(decimal(15,2),TB022*LA012) ccost,
TB022 qty
FROM  ACRTB LEFT JOIN ACRTA ON TA001=TB001 AND TA002=TB002
            LEFT JOIN COPMA ON TA004=MA001
            LEFT JOIN INVMB ON TB039=MB001
	        INNER JOIN INVLA ON LA006=TB005 AND LA007=TB006 AND LA008=TB007
            LEFT JOIN (SELECT MA003,MA002 FROM INVMA WHERE MA001='4') A1 ON A1.MA002=MB008		    
WHERE left(TA003,4) in (@year,@lastyear)  AND TB004 in ('1','2')  and TA025='Y'
) A
group by  A.cyear,A.cmonth,A.csaletype,A.ctype
order by  A.cyear,A.cmonth,A.csaletype,A.ctype asc

OPEN MAINBUSINESS  /* 打开游标 */ 
FETCH NEXT FROM MAINBUSINESS INTO @cyear, @cmonth,@csaletype, @ctype, @cmount,@ccost,@cqty
WHILE @@FETCH_STATUS = 0   /* 用WHILE循环控制游标活动*/ 
BEGIN
    if @cmonth=1 
		begin
			if @cyear=@lastyear  /*Jan*/
				begin
					insert #mainbusiness(item,saletype,Jan)values(@ctype,@csaletype,@cmount)
					insert #mainbusinesscost(item,saletype,Jan)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Jan)values(@ctype,@csaletype,@cqty)  
				end
			else
				begin
					insert #mainbusiness(item,saletype,Jan1)values(@ctype,@csaletype,@cmount)
					insert #mainbusinesscost(item,saletype,Jan1)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Jan1)values(@ctype,@csaletype,@cqty) 
				end	
		end
    if @cmonth=2 
		begin
			if @cyear=@lastyear  /*Feb*/
				begin
					insert #mainbusiness(item,saletype,Feb)values(@ctype,@csaletype,@cmount)
					insert #mainbusinesscost(item,saletype,Feb)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Feb)values(@ctype,@csaletype,@cqty)
					 
				end
			else
				begin
					insert #mainbusiness(item,saletype,Feb1)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Feb1)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Feb1)values(@ctype,@csaletype,@cqty)
				end	
		end
    if @cmonth=3 
		begin
			if @cyear=@lastyear  /*Mar*/
				begin
					insert #mainbusiness(item,saletype,Mar)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Mar)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Mar)values(@ctype,@csaletype,@cqty)
				end
			else
				begin
					insert #mainbusiness(item,saletype,Mar1)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Mar1)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Mar1)values(@ctype,@csaletype,@cqty)
				end	
		end
    if @cmonth=4 
		begin
			if @cyear=@lastyear  /*Apr*/
				begin
					insert #mainbusiness(item,saletype,Apr)values(@ctype,@csaletype,@cmount)
					insert #mainbusinesscost(item,saletype,Apr)values(@ctype,@csaletype,@ccost) 
					insert #mainbusinessqty(item,saletype,Apr)values(@ctype,@csaletype,@cqty)
				end
			else
				begin
					insert #mainbusiness(item,saletype,Apr1)values(@ctype,@csaletype,@cmount)
					insert #mainbusinesscost(item,saletype,Apr1)values(@ctype,@csaletype,@ccost) 
					insert #mainbusinessqty(item,saletype,Apr1)values(@ctype,@csaletype,@cqty)
				end	
		end
    if @cmonth=5
		begin
			if @cyear=@lastyear  /*May*/
				begin
					insert #mainbusiness(item,saletype,May)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,May)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,May)values(@ctype,@csaletype,@cqty)
				end
			else
				begin
					insert #mainbusiness(item,saletype,May1)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,May1)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,May1)values(@ctype,@csaletype,@cqty)
				end	
		end
    if @cmonth=6
		begin
			if @cyear=@lastyear  /*Jun*/
				begin
					insert #mainbusiness(item,saletype,Jun)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Jun)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Jun)values(@ctype,@csaletype,@cqty)
				end
			else
				begin
					insert #mainbusiness(item,saletype,Jun1)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Jun1)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Jun1)values(@ctype,@csaletype,@cqty)
				end	
		end
    if @cmonth=7
		begin
			if @cyear=@lastyear  /*Jul*/
				begin
				    
					insert #mainbusiness(item,saletype,Jul)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Jul)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Jul)values(@ctype,@csaletype,@cqty)
				end
			else
				begin
					insert #mainbusiness(item,saletype,Jul1)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Jul1)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Jul1)values(@ctype,@csaletype,@cqty)
				end	
		end
    if @cmonth=8
		begin
			if @cyear=@lastyear  /*Aug*/
				begin
					insert #mainbusiness(item,saletype,Aug)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Aug)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Aug)values(@ctype,@csaletype,@cqty)
				end
			else
				begin
					insert #mainbusiness(item,saletype,Aug1)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Aug1)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Aug1)values(@ctype,@csaletype,@cqty)
				end	
		end
    if @cmonth=9
		begin
			if @cyear=@lastyear  /*Sep*/
				begin
					insert #mainbusiness(item,saletype,Sep)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Sep)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Sep)values(@ctype,@csaletype,@cqty)
				end
			else
				begin
					insert #mainbusiness(item,saletype,Sep1)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Sep1)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Sep1)values(@ctype,@csaletype,@cqty)
				end	
		end
    if @cmonth=10
		begin
			if @cyear=@lastyear  /*Oct*/
				begin
					insert #mainbusiness(item,saletype,Oct)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Oct)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Oct)values(@ctype,@csaletype,@cqty)
				end
			else
				begin
					insert #mainbusiness(item,saletype,Oct1)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Oct1)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Oct1)values(@ctype,@csaletype,@cqty)
				end	
		end
    if @cmonth=11 
		begin
			if @cyear=@lastyear  /*Nov*/
				begin
					insert #mainbusiness(item,saletype,Nov)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Nov)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Nov)values(@ctype,@csaletype,@cqty)
				end
			else
				begin
					insert #mainbusiness(item,saletype,Nov1)values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,Nov1)values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,Nov1)values(@ctype,@csaletype,@cqty)
				end	
		end
    if @cmonth=12 
		begin
			if @cyear=@lastyear  /*Dec*/
				begin
					insert #mainbusiness(item,saletype,[Dec])values(@ctype,@csaletype,@cmount) 
					insert #mainbusinesscost(item,saletype,[Dec])values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,[Dec])values(@ctype,@csaletype,@cqty)
				end
			else
				begin
					insert #mainbusiness(item,saletype,Dec1)values(@ctype,@csaletype,@cmount)
					insert #mainbusinesscost(item,saletype,[Dec1])values(@ctype,@csaletype,@ccost)
					insert #mainbusinessqty(item,saletype,[Dec1])values(@ctype,@csaletype,@cqty) 
				end	
		end
	FETCH NEXT FROM MAINBUSINESS INTO @cyear, @cmonth,@csaletype, @ctype, @cmount,@ccost,@cqty /* 在循环体内将读取其余行数据 */
END
CLOSE MAINBUSINESS   /* 关闭游标 */ 
DEALLOCATE MAINBUSINESS   /* 删除游标 */ 

--/*汇总收入*/
select * into #sales from
(
SELECT '内外销' saletype,'Sales revenue' item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusiness
union all
SELECT '内销' saletype,'Domestic Sales' item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusiness
where saletype='内销' 
union all
SELECT saletype,item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusiness
where saletype='内销' 
group by item,saletype

union all
SELECT '外销' saletype,'Export Sales' item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusiness
where saletype='外销' 

union all
SELECT saletype,item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusiness
where saletype='外销' 
group by item,saletype
) a

----/*汇总成本*/
select * into #cost from(
SELECT '内外销' saletype,'Sales revenue' item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusinesscost
union all
SELECT '内销' saletype,'Domestic Sales' item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusinesscost
where saletype='内销' 
union all
SELECT saletype,item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusinesscost
where saletype='内销' 
group by item,saletype

union all
SELECT '外销' saletype,'Export Sales' item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusinesscost
where saletype='外销' 

union all
SELECT saletype,item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusinesscost
where saletype='外销' 
group by item,saletype
) b

----/*汇总数量*/
select * into #qty from
(
SELECT '内外销' saletype,'Sales revenue' item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusinessqty
union all
SELECT '内销' saletype,'Domestic Sales' item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusinessqty
where saletype='内销' 
union all
SELECT saletype,item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusinessqty
where saletype='内销' 
group by item,saletype

union all
SELECT '外销' saletype,'Export Sales' item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusinessqty
where saletype='外销' 

union all
SELECT saletype,item, sum(Jan)  Jan,sum(Jan1) Jan1,sum(Feb) Feb,sum(Feb1) Feb1,sum(Mar) Mar,sum(Mar1) Mar1,sum(Apr) Apr,sum(Apr1) Apr1,sum(May) May,sum(May1) May1,sum(Jun) Jun,sum(Jun1) Jun1,sum(Jul) Jul,
             sum(Jul1) Jul1,sum(Aug) Aug,sum(Aug1) Aug1,sum(Sep) Sep,sum(Sep1) Sep1,sum(Oct) Oct,sum(Oct1) Oct1,sum(Nov) Nov,sum(Nov1) Nov1,sum([Dec])[Dec],sum([Dec1]) [Dec1]
FROM #mainbusinessqty
where saletype='外销' 
group by item,saletype
) c

/*收入*/
select * from #sales
/*成本*/
select 
   saletype ,
  case when item='Sales revenue' then 'Cost of sales'
              when item='Domestic Sales' then 'Domestic cost'
              else item end as item, 
   Jan  ,Jan1  
  ,Feb ,Feb1 ,Mar ,Mar1 
  ,Apr ,Apr1 ,May ,May1 
  ,Jun ,Jun1 ,Jul ,Jul1 
  ,Aug ,Aug1 ,Sep ,Sep1 
  ,Oct ,Oct1 ,Nov ,Nov1 
  ,[Dec] ,[Dec1]         
from #cost

--/*数量*/
select * from #qty

--/*成本占收入比*/
select
s.saletype, 
case when s.item='Sales revenue' then 'Cost/Sales'
     when s.item='Domestic Sales' then 'Domestic'
     else s.item End as item ,
case when s.Jan<>0 then convert(decimal(10,2),c.Jan/s.Jan*100) else 0 end as Jan,case when s.Jan1<>0 then convert(decimal(10,2),c.Jan1/s.Jan1*100) else 0 end as Jan1,
case when s.Feb<>0 then convert(decimal(10,2),c.Feb/s.Feb*100) else 0 end as Feb,case when s.Feb1<>0 then convert(decimal(10,2),c.Feb1/s.Feb1*100) else 0 end as Feb1,
case when s.Mar<>0 then convert(decimal(10,2),c.Mar/s.Mar*100) else 0 end as Mar,case when s.Mar1<>0 then convert(decimal(10,2),c.Mar1/s.Mar1*100) else 0 end as Mar1,
case when s.Apr<>0 then convert(decimal(10,2),c.Apr/s.Apr*100) else 0 end as Apr,case when s.Apr1<>0 then convert(decimal(10,2),c.Apr1/s.Apr1*100) else 0 end as Apr1,
case when s.May<>0 then convert(decimal(10,2),c.May/s.May*100) else 0 end as May,case when s.May1<>0 then convert(decimal(10,2),c.May1/s.May1*100) else 0 end as May1,
case when s.Jun<>0 then convert(decimal(10,2),c.Jun/s.Jun*100) else 0 end as Jun,case when s.Jun1<>0 then convert(decimal(10,2),c.Jun1/s.Jun1*100) else 0 end as Jun1,
case when s.Jul<>0 then convert(decimal(10,2),c.Jul/s.Jul*100) else 0 end as Jul,case when s.Jul1<>0 then convert(decimal(10,2),c.Jul1/s.Jul1*100) else 0 end as Jul1,
case when s.Aug<>0 then convert(decimal(10,2),c.Aug/s.Aug*100) else 0 end as Aug,case when s.Aug1<>0 then convert(decimal(10,2),c.Aug1/s.Aug1*100) else 0 end as Aug1,
case when s.Sep<>0 then convert(decimal(10,2),c.Sep/s.Sep*100) else 0 end as Sep,case when s.Sep1<>0 then convert(decimal(10,2),c.Sep1/s.Sep1*100) else 0 end as Sep1,
case when s.Oct<>0 then convert(decimal(10,2),c.Oct/s.Oct*100) else 0 end as Oct,case when s.Oct1<>0 then convert(decimal(10,2),c.Oct1/s.Oct1*100) else 0 end as Oct1,
case when s.Nov<>0 then convert(decimal(10,2),c.Nov/s.Nov*100) else 0 end as Nov,case when s.Nov1<>0 then convert(decimal(10,2),c.Nov1/s.Nov1*100) else 0 end as Nov1,
case when s.[Dec]<>0 then convert(decimal(10,2),c.[Dec]/s.[Dec]) else 0 end as [Dec],case when s.[Dec1]<>0 then convert(decimal(10,2),c.[Dec1]/s.[Dec1])  else 0 end as [Dec1]
from #sales  s inner join  #cost  c  on s.item=c.item and s.saletype=c.saletype
--/*毛利润*/
select 
s.saletype,
case when s.item='Sales revenue' then 'Profit' 
	 when s.item='Domestic Sales' then 'Domestic profit'  
     else s.item End as item ,
s.Jan-c.Jan as Jan,s.Jan1-c.Jan1 as Jan1,
s.Feb-c.Feb as Feb,s.Feb1-c.Feb1 as Feb1,
s.Mar-c.Mar as Mar,s.Mar1-c.Mar1 as Mar1,
s.Apr-c.Apr as Apr,s.Apr1-c.Apr1 as Apr1,
s.May-c.May as May,s.May1-c.May1 as May1,
s.Jun-c.Jun as Jun,s.Jun1-c.Jun1 as Jun1,
s.Jul-c.Jul as Jul,s.Jul1-c.Jul1 as Jul1,
s.Aug-c.Aug as Aug,s.Aug1-c.Aug1 as Aug1,
s.Sep-c.Sep as Sep,s.Sep1-c.Sep1 as Sep1,
s.Oct-c.Oct as Oct,s.Oct1-c.Oct1 as Oct1,
s.Nov-c.Nov as Nov,s.Nov1-c.Nov1 as Nov1,
s.[Dec]-c.[Dec] as [Dec],s.[Dec1]-c.[Dec1] as [Dec1]
from #sales  s inner join  #cost  c  on s.item=c.item and s.saletype=c.saletype
--/*毛利率*/
select 
s.saletype,
case when s.item='Sales revenue' then 'Profit margin' 
	 when s.item='Domestic Sales' then 'Domestic'  
     else s.item End as item ,
case when s.Jan<>0 then convert(decimal(10,2),(s.Jan-c.Jan)/s.Jan*100) else 0 end as Jan,case when s.Jan1<>0 then convert(decimal(10,2),(s.Jan1-c.Jan1)/s.Jan1*100) else 0 end as Jan1,
case when s.Feb<>0 then convert(decimal(10,2),(s.Feb-c.Feb)/s.Feb*100) else 0 end as Feb,case when s.Feb1<>0 then convert(decimal(10,2),(s.Feb1-c.Feb1)/s.Feb1*100) else 0 end as Feb1,
case when s.Mar<>0 then convert(decimal(10,2),(s.Mar-c.Mar)/s.Mar*100) else 0 end as Mar,case when s.Mar1<>0 then convert(decimal(10,2),(s.Mar1-c.Mar1)/s.Mar1*100) else 0 end as Mar1,
case when s.Apr<>0 then convert(decimal(10,2),(s.Apr-c.Apr)/s.Apr*100) else 0 end as Apr,case when s.Apr1<>0 then convert(decimal(10,2),(s.Apr1-c.Apr1)/s.Apr1*100) else 0 end as Apr1,
case when s.May<>0 then convert(decimal(10,2),(s.May-c.May)/s.May*100) else 0 end as May,case when s.May1<>0 then convert(decimal(10,2),(s.May1-c.May1)/s.May1*100) else 0 end as May1,
case when s.Jun<>0 then convert(decimal(10,2),(s.Jun-c.Jun)/s.Jun*100) else 0 end as Jun,case when s.Jun1<>0 then convert(decimal(10,2),(s.Jun1-c.Jun1)/s.Jun1*100) else 0 end as Jun1,
case when s.Jul<>0 then convert(decimal(10,2),(s.Jul-c.Jul)/s.Jul*100) else 0 end as Jul,case when s.Jul1<>0 then convert(decimal(10,2),(s.Jul1-c.Jul1)/s.Jul1*100) else 0 end as Jul1,
case when s.Aug<>0 then convert(decimal(10,2),(s.Aug-c.Aug)/s.Aug*100) else 0 end as Aug,case when s.Aug1<>0 then convert(decimal(10,2),(s.Aug1-c.Aug1)/s.Aug1*100) else 0 end as Aug1,
case when s.Sep<>0 then convert(decimal(10,2),(s.Sep-c.Sep)/s.Sep*100) else 0 end as Sep,case when s.Sep1<>0 then convert(decimal(10,2),(s.Sep1-c.Sep1)/s.Sep1*100) else 0 end as Sep1,
case when s.Oct<>0 then convert(decimal(10,2),(s.Oct-c.Oct)/s.Oct*100) else 0 end as Oct,case when s.Oct1<>0 then convert(decimal(10,2),(s.Oct1-c.Oct1)/s.Oct1*100) else 0 end as Oct1,
case when s.Nov<>0 then convert(decimal(10,2),(s.Nov-c.Nov)/s.Nov*100) else 0 end as Nov,case when s.Nov1<>0 then convert(decimal(10,2),(s.Nov1-c.Nov1)/s.Nov1*100) else 0 end as Nov1,
case when s.[Dec]<>0 then convert(decimal(10,2),(s.[Dec]-c.[Dec])/s.[Dec]*100) else 0 end as [Dec],case when s.[Dec1]<>0 then convert(decimal(10,2),(s.[Dec1]-c.[Dec1])/s.[Dec1]*100)  else 0 end as[Dec1]
from #sales  s inner join  #cost  c  on s.item=c.item and s.saletype=c.saletype


drop table #mainbusiness
drop table #mainbusinesscost
drop table #mainbusinessqty
drop table #sales
drop table #cost
drop table #qty

end


[置顶] [易飞]主营业务分析_第1张图片[置顶] [易飞]主营业务分析_第2张图片

你可能感兴趣的:(JOIN,Date,object,table,insert,UP)