-- 作者:fubblyc
-- 发布时间:2016/5/29 15:22:00
-- 子查询不能分组??
如图,【本月累计销售额】是指各个营业员的自己的累计销售额,但是结果是所有营业员总的累计销售额,没有按 营业员 进行分组。
要怎么才能实现 营业员分组呢?
此主题相关图片如下:qq截图20160529152455.png
代码:
SELEC T cast(datename(year,a.[业务日期])as int) AS [年份], cast(datename(month,a.[业务日期])as int) AS [月份], cast(datename(week,a.[业务日期])as int) AS [周次], a.[营业员姓名], sum(a.[销售额]) as 本周销售额 , (selec t sum(b.[销售额]) from dbo.worksheet as b where datename(week,b.[业务日期])<=datename(week,a.[业务日期]) and datename(year,b.[业务日期]) = datename(year,a.[业务日期]) and datename(month,b.[业务日期]) = datename(month,a.[业务日期])) AS [本月累计销售额]
FROM dbo.worksheet as a GROUP BY cast(datename(year,a.[业务日期])as int) , cast(datename(month,a.[业务日期])as int) , cast(datename(week,a.[业务日期])as int) , a.[营业员姓名], datename(year,a.[业务日期]), datename(month,a.[业务日期]), datename(week,a.[业务日期])
[此贴子已经被作者于2016/5/29 15:25:18编辑过]
|