关于分组及占百分比的sql语句
2010-12-21 11:27
375 查看
视图部份:
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
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
相关文章推荐
- 关于分组及占百分比的sql语句
- oracle中,分组后,取各组的前n条记录的sql语句:rownumber() over()
- 如何通过SQL语句获取分组中的某几条记录
- 关于DbVisualizer 6.0.14中的SQL语句显示中文问题
- 关于SQL中Select语句的执行顺序
- 关于多表查询sql常用的连接语句:左外连接、右外连接、内连接
- 关于SQL语句的性能
- SQL语句学习(2)之基本查询、排序、聚合函数、分组查询
- 关于使用一条SQL语句 找出同时符合多个tag条件的记录集合算法
- 关于SQL语句的多表更新有哪位高手知道?
- 关于SQL语句中的双引号、单引号和&
- 关于No Dialect mapping for JDBC type :-9 hibernate执行原生sql语句问题
- 学习 SQL 语句 - Select(8): 分组条件
- 关于过去工作的一些总结:常用sql语句以及数据库查询优化
- GIS应用开发中常用的SQL语句(分组排序取前几条记录)
- 关于sql语句区分大小写问题
- 关于用sql语句select多个字段,该使用什么接收的解决办法
- sql中关于分组(group by)的理解
- 关于SQL语句外连接中的过滤条件
- SQL语句:查询出每个分组的前若干条记录