一、要求
1、表结构如下:
ID DepartName(部门) Sales(销售量) SalesDate(销售日期)
1 营销一部 300 2006-7-1
2 营销二部 500 2006-7-1
3 营销三部 800 2006-8-1
4 营销一部 600 2006-8-1
5 营销二部 800 2006-8-1
6 营销一部 400 2007-7-1
7 营销二部 800 2007-7-1
8 营销三部 700 2007-8-1
9 营销一部 600 2008-7-1
10 营销二部 300 2008-7-1
2、要根据要求得到以下数据
1)得到同比数据
部门 对比年月 本月销售总量 去年同期销售总量 同比
营销一部 2006-07-01 300.00 NULL 无穷大
营销二部 2006-07-01 500.00 NULL 无穷大
营销三部 2006-08-01 800.00 NULL 无穷大
营销一部 2006-08-01 600.00 NULL 无穷大
营销二部 2006-08-01 800.00 NULL 无穷大
营销一部 2007-07-01 400.00 300.00 33.33%
营销二部 2007-07-01 800.00 500.00 60.00%
营销三部 2007-08-01 700.00 800.00 -12.50%
营销一部 2008-07-01 600.00 400.00 50.00%
营销二部 2008-07-01 700.00 800.00 -12.50%
(2)选择月份 获取环比数据
部门 对比年月 本月销售总量 上月销售总量 环比
营销一部 2006-07-01 300.00 NULL 无穷大
营销二部 2006-07-01 500.00 NULL 无穷大
营销三部 2006-08-01 800.00 NULL 无穷大
营销一部 2006-08-01 600.00 300.00 100.00%
营销二部 2006-08-01 800.00 500.00 60.00%
营销一部 2007-07-01 400.00 NULL 无穷大
营销二部 2007-07-01 800.00 NULL 无穷大
营销三部 2007-08-01 700.00 NULL 无穷大
营销一部 2008-07-01 600.00 NULL 无穷大
营销二部 2008-07-01 700.00 NULL 无穷大
二、代码:
1、创建表
CREATE TABLE SalesDetail(
ID int, –序号
DepartName varchar(50), –部门
Sales decimal(10,2), –销售量
SalesDate Datetime –销售日期
)
2、插入测试数据
insert into SalesDetail(ID, DepartName, Sales, SalesDate)
select 1, ‘营销一部’, 300, ‘2006-7-1’
UNION ALL select 2, ‘营销二部’, 500, ‘2006-7-1’
UNION ALL select 3, ‘营销三部’, 800, ‘2006-8-1’
UNION ALL select 4, ‘营销一部’, 600, ‘2006-8-1’
UNION ALL select 5, ‘营销二部’, 800, ‘2006-8-1’
UNION ALL select 6, ‘营销一部’, 400, ‘2007-7-1’
UNION ALL select 7, ‘营销二部’, 800, ‘2007-7-1’
UNION ALL select 8, ‘营销三部’, 700, ‘2007-8-1’
UNION ALL select 9, ‘营销一部’, 600, ‘2008-7-1’
UNION ALL select 10, ‘营销二部’, 700, ‘2008-7-1’
3、实现SQL代码
//得到环比
select c.DepartName as 部门, convert(varchar(10), c.SalesDate, 23) as 对比年月, c.Sales as 本月销售总量, d.Sales as 上月销售总量, case when d.Sales is null or d.Sales=0 then ‘无穷大’ else cast(cast((isnull(c.Sales, 0)-isnull(d.Sales,0))*100/isnull(d.Sales, 0) as decimal(10,2)) as varchar(50))+’%’ end as 环比 from SalesDetail c left join
(select a.DepartName as DepartName, a.Sales as Sales, a.SalesDate as lsSalesDate, b.SalesDate as SalesDate from SalesDetail a join SalesDetail b on a.SalesDate=DateAdd(month, -1, b.SalesDate) and a.DepartName=b.DepartName) d
on c.SalesDate=d.SalesDate and c.DepartName=d.DepartName
//得到同比
select c.DepartName as 部门, convert(varchar(10), c.SalesDate, 23) as 对比年月, c.Sales as 本月销售总量, d.Sales as 去年同期销售总量, case when d.Sales is null or d.Sales=0 then ‘无穷大’ else cast(cast((isnull(c.Sales, 0)-isnull(d.Sales,0))*100/isnull(d.Sales, 0) as decimal(10,2)) as varchar(50))+’%’ end as 同比 from SalesDetail c left join
(select a.DepartName as DepartName, a.Sales as Sales, a.SalesDate as lsSalesDate, b.SalesDate as SalesDate from SalesDetail a join SalesDetail b on a.SalesDate=DateAdd(year, -1, b.SalesDate) and a.DepartName=b.DepartName) d
on c.SalesDate=d.SalesDate and c.DepartName=d.DepartName
4、SQL语句说明
//获取根据时间获取去年同期时间
select DateAdd(year, -1, SalesDate)
//获取根据时间获取上月时间
select DateAdd(month, -1, SalesDate)
//获取根据时间获取去年同期时间数据
select a.DepartName as DepartName, a.Sales as Sales, a.SalesDate as lsSalesDate, b.SalesDate as SalesDate from SalesDetail a join SalesDetail b on a.SalesDate=DateAdd(year, -1, b.SalesDate) and a.DepartName=b.DepartName
//获取根据时间获取上月时间数据
select a.DepartName as DepartName, a.Sales as Sales, a.SalesDate as lsSalesDate, b.SalesDate as SalesDate from SalesDetail a join SalesDetail b on a.SalesDate=DateAdd(month, -1, b.SalesDate) and a.DepartName=b.DepartName