关于分组及占百分⽐的sql语句
视图部份:
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 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  and isnull(FYorNDel,0)<>1
)b order by a.总⾦额 Desc
isnull的用法GO

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。