周报(按销售组)

select 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,

    收入环比增加=((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)

    left 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))/(select sum(Income)

from Mobi_Fact_KPI

    left join [Time]

    on Convert(nvarchar,[Mobi_Fact_KPI].RunDate,112)=Convert(nvarchar,[Time].PK_Date,112)

    left 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)),

    本周收入=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)

    left 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)

from Mobi_Fact_KPI as a

    left join [Time] as b

    on Convert(nvarchar,a.RunDate,112)=Convert(nvarchar,b.PK_Date,112)

    left 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

你可能感兴趣的:(周报(按销售组))