# [易飞]财务表之销售分析

USE [ZM]
GO
/****** Object:  StoredProcedure [dbo].[UP_SalesAnalyis]    Script Date: 03/31/2012 08:27:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <David Gang>
-- Create date: <2012-03-30>
-- Description: <销售分析按区域客户产品>
-- =============================================

ALTER Procedure [dbo].[UP_SalesAnalyis]
as
begin

declare @yyyymm char(6)
set @yyyymm=datepart(year,getdate())*100+datepart(month,getdate())
------------------------------销售区域--------------------------------
select Customers,
sum(Quantities) Quantities,
sum(Amount) Amount,
sum([Cost of Sales]) as [Cost of Sales],
sum(Amount)-sum([Cost of Sales]) as [Gross Margin]
into #base from (
SELECT
ltrim(MR003) Customers,
convert(decimal(15,2),TB022) as Quantities,
TB019 as Amount,
convert(decimal(15,2),TB022*LA012) as [Cost of Sales]
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 MR002,MR003 from CMSMR where MR001='2') R ON MA076=R.MR002
WHERE left(TA003,6)=@yyyymm  AND TB004 in ('1','2')  and TA025='Y'
) a
group by Customers
order by Customers

select * into #basetotal from (
select Customers,Quantities,Amount,[Cost of Sales],[Gross Margin] from #base
union all
select 'total' as Customers,sum(Quantities) Quantities,sum(Amount) Amount,sum([Cost of Sales]) [Cost of Sales],sum([Gross Margin]) [Gross Margin] from #base
)a

declare @totalAmount as decimal(10,2)
declare @totalGrossMargin as decimal(10,2)
select @totalAmount=sum(Amount),@totalGrossMargin=sum([Gross Margin]) from #base
--销售月度按区域汇总
select Customers,Quantities,Amount,[Cost of Sales],[Gross Margin],
convert(decimal(10,2),[Gross Margin]/Amount*100) as GMR,
convert(decimal(10,2),Amount/@totalAmount*100) as [Sales%],
convert(decimal(10,2),[Gross Margin]/@totalGrossMargin*100) as [GM%]
from #basetotal
--销售构成
select
Customers,
convert(decimal(10,2),Amount/@totalAmount*100) as [Sales%]
from #basetotal
where Customers<>'total'
--利润构成
select
Customers,
convert(decimal(10,2),[Gross Margin]/@totalGrossMargin*100) as [GM%]
from #basetotal
where Customers<>'total'

------------------------------统计前10大客户--------------------------------
select top 10 Customers,
sum(Quantities) Quantities,
sum(Amount) Amount,
sum([Cost of Sales]) as [Cost of Sales],
sum(Amount)-sum([Cost of Sales]) as [Gross Margin]
into #basetop10
from (
SELECT
COPMA.MA002 as  Customers,
convert(decimal(15,2),TB022) as Quantities,
TB019 as Amount,
convert(decimal(15,2),TB022*LA012) as [Cost of Sales]

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
WHERE left(TA003,6)=@yyyymm  AND TB004 in ('1','2')  and TA025='Y'
) b
group by Customers
order by Amount desc

--汇总前十+小计
select * into #totaltop10 from (
select Customers,Quantities,Amount,[Cost of Sales],[Gross Margin] from #basetop10
union all
select 'total' as Customers,sum(Quantities) Quantities,sum(Amount) Amount,sum([Cost of Sales]) [Cost of Sales],sum([Gross Margin]) [Gross Margin] from #basetop10
)c

--前十大数据表
select Customers,Quantities,Amount,[Cost of Sales],[Gross Margin],
convert(decimal(10,2),[Gross Margin]/Amount*100) as GMR,
convert(decimal(10,2),Amount/@totalAmount*100) as [Sales%],
convert(decimal(10,2),[Gross Margin]/@totalGrossMargin*100) as [GM%]
from #totaltop10
--前十大客户数据图表数据
select * from #totaltop10
where Customers<>'total'

---------------------------------------前十大产品销售--------------------------------------------

select top 10 Product,
sum(Quantities) Quantities,
sum(Amount) Amount,
sum([Cost of Sales]) as [Cost of Sales],
sum(Amount)-sum([Cost of Sales]) as [Gross Margin]
into #ProductTop10
from (
SELECT  TB040 Product,convert(decimal(15,2),TB022) Quantities,TB019 Amount, convert(decimal(15,2),TB022*LA012)[Cost of Sales]
FROM  ACRTB left JOIN ACRTA ON TA001=TB001 AND TA002=TB002
inner JOIN INVLA ON LA006=TB005 AND LA007=TB006 AND LA008=TB007
WHERE left(TA003,6)=@yyyymm  AND TB004 in ('1','2')  and TA025='Y'
) d
group by Product
order by Amount desc

declare @subtotalAmount as decimal(10,2)
declare @subtotalQuantities as decimal(10,2)
declare @subtotalCost as decimal(10,2)
declare @subtotalGrossMargin as decimal(10,2)
select @subtotalQuantities=sum(Quantities),@subtotalAmount=sum(Amount),@subtotalCost=sum([Cost of Sales]),@subtotalGrossMargin=sum([Gross Margin]) from #ProductTop10

--汇总产品总计 小计，其他
select *
into #totalProducttop10
from (
select Product,Quantities,Amount,[Cost of Sales],[Gross Margin] from #ProductTop10
union all
select 'SubTotal' as Product,sum(Quantities) Quantities,sum(Amount) Amount,sum([Cost of Sales]) [Cost of Sales],sum([Gross Margin]) [Gross Margin] from #ProductTop10
union all
SELECT 'Others' as Product, sum(convert(decimal(15,2),TB022))-@subtotalQuantities AS Quantities,
sum(TB019)-@subtotalAmount AS  Amount,
convert(decimal(15,2),sum(TB022*LA012))-@subtotalCost [Cost of Sales],
sum(TB019)-convert(decimal(15,2),sum(TB022*LA012))-@subtotalGrossMargin as [Gross Margin]
FROM  ACRTB left JOIN ACRTA ON TA001=TB001 AND TA002=TB002
inner JOIN INVLA ON LA006=TB005 AND LA007=TB006 AND LA008=TB007
WHERE left(TA003,6)=@yyyymm  AND TB004 in ('1','2')  and TA025='Y'
union all
SELECT 'Total' as Product, sum(convert(decimal(15,2),TB022)) Quantities,sum(TB019)  Amount, convert(decimal(15,2),sum(TB022*LA012)) [Cost of Sales],
sum(TB019)-convert(decimal(15,2),sum(TB022*LA012)) as [Gross Margin]
FROM  ACRTB left JOIN ACRTA ON TA001=TB001 AND TA002=TB002
inner JOIN INVLA ON LA006=TB005 AND LA007=TB006 AND LA008=TB007
WHERE left(TA003,6)=@yyyymm  AND TB004 in ('1','2')  and TA025='Y'
)c
--十大产品汇总表
select Product,Quantities,Amount,[Cost of Sales],[Gross Margin],
convert(decimal(10,2),[Gross Margin]/Amount*100) as GMR,
convert(decimal(10,2),Amount/@totalAmount*100) as [Sales%],
convert(decimal(10,2),[Gross Margin]/@totalGrossMargin*100) as [GM%]
from #totalProducttop10
--十大产品销售收入占比
select top 10 Product,
convert(decimal(10,2),Amount/@totalAmount*100) as [Sales%]
from #totalProducttop10
--十大产品毛利占比
select top 10 Product,
convert(decimal(10,2),[Gross Margin]/@totalGrossMargin*100) as [GM%]
from #totalProducttop10

drop table #base
drop table #basetotal
drop table #totaltop10
drop table #basetop10
drop table  #ProductTop10
drop table #totalProducttop10
end

## 易飞ERP--自定义报表之采购价格分析功能表

• ymruseuse
• 2017年11月17日 15:50
• 858

## 采购接收未匹配报表

• zhanzhib
• 2015年03月31日 09:23
• 523

## 易飞采购降涨价分析软件

• 2014年02月26日 09:16
• 1.06MB
• 下载

## [易飞]销售成本分析

• david_520042
• 2011年02月23日 17:00
• 1631

## [易飞]指定日期结存

• david_520042
• 2015年11月12日 15:54
• 2042

## [易飞]付款条件-账期之理解

[摘要]通常付款条件设置好后,不会轻易去变动。在变动修改的时候，我们得了解下付款条件的含义。以便更加准确的设置。一、加日数 X天 加日数：根据起算日（1.开票日2.次月初）+天数       收款日=...
• david_520042
• 2016年04月05日 12:51
• 3472

## [易飞]易飞ERP展BOM带成本+替代料

-- ============================================= -- Author: -- Create date: -- D...
• david_520042
• 2015年05月11日 08:51
• 2613

## [易飞]自定义会计报表从A帐套复制到B帐套

【背景】通常我们在开立新帐套时，由于新公司的业务模式和原公司比较类似，通常会拷贝基础资料到新帐套。那如何进行批量导入呢？ 在之前的文章提到过几中方法： 1. 利用SQL导入 2. 利用...
• david_520042
• 2017年06月19日 20:04
• 476

## [易飞]自定义报表重命名编码

• david_520042
• 2016年04月07日 20:53
• 3147

## 易飞打开报表时报：开启报表失败或者其他错误

】将CONDUCTORC.INI中的COMPUTERNAME改为127.0.0.1，并用数据检视器将直接查询的格式档备份删除后查询正常...