周报(二)

select top 30 ProductName as 产品,收入=sum(Income),占比=sum(Income)/(select SUM(Income) from [Mobi_Fact_KPI]) from Mobi_Fact_KPI
left join Mobi_Dim_Order
on [Mobi_Fact_KPI].OrderID=[Mobi_Dim_Order].OrderID
group by ProductName
order by sum(Income) desc


select top 30 ProductName as 产品,毛利=(sum(Income)-sum(Expend)),占比=(sum(Income)-sum(Expend))/(select (sum(Income)-sum(Expend)) from [Mobi_Fact_KPI]) from Mobi_Fact_KPI
left join Mobi_Dim_Order
on [Mobi_Fact_KPI].OrderID=[Mobi_Dim_Order].OrderID
group by ProductName
order by (sum(Income)-sum(Expend)) desc


--当前周=(select #temp_CurrentWeek.OperateWeek_Of_Year from #temp_CurrentWeek)
--获取当前周
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#temp_CurrentWeek') and type='U')
drop table #temp_CurrentWeek
go
select a.OperateWeek_Of_Year into #temp_CurrentWeek from (select OperateWeek_Of_Year from Time
where Convert(nvarchar,[Time].PK_Date,112)=Convert(nvarchar,CURRENT_TIMESTAMP,112)) as a
--上周收入
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#temp_LastWeekIncomeBySalesGroup') and type='U')
drop table #temp_LastWeekIncomeBySalesGroup
go
select a.销售组,a.上周收入 into #temp_LastWeekIncomeBySalesGroup from (select [Mobi_Dim_Order].Salesgroup as 销售组,sum(Income) as 上周收入
from Mobi_Fact_KPI
join [Time]
on Convert(nvarchar,[Mobi_Fact_KPI].RunDate,112)=Convert(nvarchar,[Time].PK_Date,112)
join [Mobi_Dim_Order]
on [Mobi_Fact_KPI].OrderID=[Mobi_Dim_Order].OrderID
where [Time].Week_Of_Year= ((select OperateWeek_Of_Year from Time
where Convert(nvarchar,[Time].PK_Date,112)=Convert(nvarchar,CURRENT_TIMESTAMP,112))-1)
group by [Mobi_Dim_Order].Salesgroup) as a

--上上周收入
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#temp_LastTwoWeeksIncomeBySalesGroup') and type='U')
drop table #temp_LastTwoWeeksIncomeBySalesGroup
go
select b.销售组,b.上上周收入 into #temp_LastTwoWeeksIncomeBySalesGroup from (select [Mobi_Dim_Order].Salesgroup as 销售组,sum(Income) as 上上周收入
from Mobi_Fact_KPI
join [Time]
on Convert(nvarchar,[Mobi_Fact_KPI].RunDate,112)=Convert(nvarchar,[Time].PK_Date,112)
join [Mobi_Dim_Order]
on [Mobi_Fact_KPI].OrderID=[Mobi_Dim_Order].OrderID
where [Time].Week_Of_Year= ((select OperateWeek_Of_Year from Time
where Convert(nvarchar,[Time].PK_Date,112)=Convert(nvarchar,CURRENT_TIMESTAMP,112))-2)
group by [Mobi_Dim_Order].Salesgroup) as b


--销售组维度周报
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#temp_OriginalBySalesGroup') and type='U')
drop table #temp_OriginalBySalesGroup
go
select d.当前周,d.销售组,d.收入,d.收入占比,d.支出,d.返点前毛利,d.返点前利润率,d.收入环比增加,d.上周收入,d.上上周收入 into #temp_OriginalBySalesGroup from
(select 当前周=(select #temp_CurrentWeek.OperateWeek_Of_Year from #temp_CurrentWeek),
c.Salesgroup as 销售组,
sum(Income) as 收入,
收入占比=sum(Income)/(select SUM(Income) from Mobi_Fact_KPI left join [Time] on Convert(nvarchar,[Mobi_Fact_KPI].RunDate,112)=Convert(nvarchar,[Time].PK_Date,112) where [Time].Week_Of_Year=(select OperateWeek_Of_Year from Time
where Convert(nvarchar,[Time].PK_Date,112)=Convert(nvarchar,CURRENT_TIMESTAMP,112))),
sum(Expend) as 支出,
(sum(Income)-sum(Expend)) as 返点前毛利,
返点前利润率=case when SUM(Income)<>0 then (sum(Income)-sum(Expend))/sum(Income) else Null end,
收入环比增加=(case when (select 上周收入 from #temp_LastWeekIncomeBySalesGroup where 销售组=c.Salesgroup)<>0 then ((select 上周收入 from #temp_LastWeekIncomeBySalesGroup where 销售组=c.Salesgroup)-(select 上周收入 from #temp_LastWeekIncomeBySalesGroup where 销售组=c.Salesgroup))/(select 上周收入 from #temp_LastWeekIncomeBySalesGroup where 销售组=c.Salesgroup) else Null end)-
(case when (select 上上周收入 from #temp_LastTwoWeeksIncomeBySalesGroup where 销售组=c.Salesgroup)<>0 then (SUM(Income)-(select 上上周收入 from #temp_LastTwoWeeksIncomeBySalesGroup where 销售组=c.Salesgroup))/(select 上上周收入 from #temp_LastTwoWeeksIncomeBySalesGroup where 销售组=c.Salesgroup) else Null end),
上周收入=(select 上周收入 from #temp_LastWeekIncomeBySalesGroup where 销售组=c.Salesgroup),
上上周收入=(select 上上周收入 from #temp_LastTwoWeeksIncomeBySalesGroup where 销售组=c.Salesgroup)
from Mobi_Fact_KPI as a
join [Time] as b
on Convert(nvarchar,a.RunDate,112)=Convert(nvarchar,b.PK_Date,112)
join [Mobi_Dim_Order] as c
on a.OrderID=c.OrderID
where b.Week_Of_Year= (select OperateWeek_Of_Year from Time
where Convert(nvarchar,[Time].PK_Date,112)=Convert(nvarchar,CURRENT_TIMESTAMP,112))
group by c.Salesgroup) as d

--更新销售组维度周报
Insert into [dbo].[Mobi_WeeklyReport_BySalesGroup](WeekOfYear, SalesGroup, Income, IncomeProportion, Expend, GrossProfitBeforeRebate, ProfitRateBeforeRebate, IncomeLinkrelativeratioAdd, LastWeekIncome, LastTwoWeeksIncome)
select 当前周,销售组,收入,收入占比,支出,返点前毛利,返点前利润率,收入环比增加,上周收入,上上周收入 from #temp_OriginalBySalesGroup

 

 

 

 


USE [sageBI]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Mobi_WeeklyReport_IncomeTop30]') AND type in (N'U'))
Drop Table [dbo].[Mobi_WeeklyReport_IncomeTop30]
GO
CREATE TABLE [dbo].[Mobi_WeeklyReport_IncomeTop30](
WeekOfYear int,
ProductName nvarchar(255) Null,
Income float Null,
IncomeProportion float Null
)
Go
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Mobi_WeeklyReport_GrossProfitTop30]') AND type in (N'U'))
Drop Table [dbo].[Mobi_WeeklyReport_GrossProfitTop30]
GO
CREATE TABLE [dbo].[Mobi_WeeklyReport_GrossProfitTop30](
WeekOfYear int,
ProductName nvarchar(255) Null,
GrossProfit float Null,
GrossProfitProportion float Null
)
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Mobi_WeeklyReport_BySalesGroup]') AND type in (N'U'))
Drop Table [dbo].[Mobi_WeeklyReport_BySalesGroup]
GO
CREATE TABLE [dbo].[Mobi_WeeklyReport_BySalesGroup](
WeekOfYear int,
SalesGroup nvarchar(255) Null,
Income float Null,
IncomeProportion float Null,
Expend float Null,
GrossProfitBeforeRebate float Null,
ProfitRateBeforeRebate float Null,
IncomeLinkrelativeratioAdd float Null
)
GO

--获取当前周
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#temp_CurrentWeek') and type='U')
drop table #temp_CurrentWeek
go
select a.OperateWeek_Of_Year into #temp_CurrentWeek from (select OperateWeek_Of_Year from Time
where Convert(nvarchar,[Time].PK_Date,112)=Convert(nvarchar,CURRENT_TIMESTAMP,112)) as a

--获取收入top30
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#temp_IncomeTop30') and type='U')
drop table #temp_IncomeTop30
go
select b.当前周,b.产品,b.收入,b.占比 into #temp_IncomeTop30 from (select top 30 当前周=(select #temp_CurrentWeek.OperateWeek_Of_Year from #temp_CurrentWeek),ProductName as 产品,收入=sum(Income),占比=sum(Income)/(select SUM(Income) from [Mobi_Fact_KPI]) from Mobi_Fact_KPI
left join Mobi_Dim_Order
on [Mobi_Fact_KPI].OrderID=[Mobi_Dim_Order].OrderID
group by ProductName
order by sum(Income) desc) as b

--更新收入top30周报表
Insert into [dbo].[Mobi_WeeklyReport_IncomeTop30](WeekOfYear, ProductName, Income, IncomeProportion)
select 当前周,产品,收入,占比 from #temp_IncomeTop30

 

 

--当前周=(select #temp_CurrentWeek.OperateWeek_Of_Year from #temp_CurrentWeek)
--获取当前周
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#temp_CurrentWeek') and type='U')
drop table #temp_CurrentWeek
go
select a.OperateWeek_Of_Year into #temp_CurrentWeek from (select OperateWeek_Of_Year from Time
where Convert(nvarchar,[Time].PK_Date,112)=Convert(nvarchar,CURRENT_TIMESTAMP,112)) as a

--获取毛利top30
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#temp_GrossProfitTop30') and type='U')
drop table #temp_GrossProfitTop30
go
select b.当前周,b.产品,b.毛利,b.占比 into #temp_GrossProfitTop30 from (select top 30 当前周=(select #temp_CurrentWeek.OperateWeek_Of_Year from #temp_CurrentWeek), ProductName as 产品,毛利=(sum(Income)-sum(Expend)),占比=(sum(Income)-sum(Expend))/(select (sum(Income)-sum(Expend)) from [Mobi_Fact_KPI]) from Mobi_Fact_KPI
left join Mobi_Dim_Order
on [Mobi_Fact_KPI].OrderID=[Mobi_Dim_Order].OrderID
group by ProductName
order by (sum(Income)-sum(Expend)) desc) as b

--更新毛利top30周报表
Insert into [dbo].[Mobi_WeeklyReport_GrossProfitTop30](WeekOfYear, ProductName, GrossProfit, GrossProfitProportion)
select 当前周,产品,毛利,占比 from #temp_GrossProfitTop30

你可能感兴趣的:(周报(二))