关于分组及占百分比的sql语句

时间:2022-05-13 23:22:39

视图部份:

SELECT dbo.t_Customer.FCustomerId, dbo.t_Customer.FCustName,
      dbo.t_SellOrderEntry.FQty, dbo.t_SellOrderEntry.FAmount, dbo.t_SellOrder.FBillDate,
      dbo.t_SellOrder.FYOrNDel
FROM dbo.t_SellOrder INNER JOIN
      dbo.t_SellOrderEntry ON
      dbo.t_SellOrder.FBillId = dbo.t_SellOrderEntry.FBillId INNER JOIN
      dbo.t_Customer ON dbo.t_SellOrder.FFirstCustId = dbo.t_Customer.FCustomerId

 

存储过程:


--求客户订单百分比
CREATE   PROCEDURE  sp_SellPecent
@yn varchar (50)
AS
select 客户id,客户名称,总数量,a.总金额,convert(char(5),Convert(decimal(9,2),round(a.总金额/b.总金额,4)*100))+'%' as 占总金额比例 from
 (select fcustomerid as 客户id,fcustname as  客户名称,sum(fqty) as 总数量,sum(famount) as 总金额  from v_订单总额排行榜
where Convert(char(4), datepart(year,fbilldate ))+ Convert(char(4), datepart(month,fbilldate)) like '%'+@yn+'%' and isnull(FYorNDel,0)<>1
group by fcustomerid,fcustname )  a,

(select sum(famount) as 总金额  from v_订单总额排行榜
where Convert(char(4), datepart(year,fbilldate ))+ Convert(char(4), datepart(month,fbilldate)) like  +'%'+@yn+'%' and isnull(FYorNDel,0)<>1

)b order by a.总金额 Desc
GO