同期及上期数据对比处理示例.sql
生活随笔
收集整理的這篇文章主要介紹了
同期及上期数据对比处理示例.sql
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
--雇員數(shù)據(jù)
CREATE TABLE Employee(
ID int,????????????? --雇員編號(hào)(主鍵)
Name nvarchar(10),?? --雇員名稱
Dept nvarchar(10))?? --所屬部門
INSERT Employee SELECT 1,N'張三',N'大客戶部'
UNION? ALL????? SELECT 2,N'李四',N'大客戶部'
UNION? ALL????? SELECT 3,N'王五',N'銷售一部'
--費(fèi)用表
CREATE TABLE Expenses(
EmployeeID int,??????? --雇員編號(hào)
Date Datetime,???????? --發(fā)生日期
Expenses nvarchar(10), --指標(biāo)名稱
[Money] decimal(10,2)) --發(fā)生金額
INSERT Expenses SELECT 1,'2004-01-01',N'銷售',100
UNION? ALL????? SELECT 1,'2004-01-02',N'銷售',150
UNION? ALL????? SELECT 1,'2004-12-01',N'銷售',200
UNION? ALL????? SELECT 1,'2005-01-10',N'銷售', 80
UNION? ALL????? SELECT 1,'2005-01-15',N'銷售', 90
UNION? ALL????? SELECT 1,'2005-01-21',N'成本',? 8
UNION? ALL????? SELECT 2,'2004-12-01',N'成本',? 2
UNION? ALL????? SELECT 2,'2005-01-10',N'銷售', 10
UNION? ALL????? SELECT 2,'2005-01-15',N'銷售', 40
UNION? ALL????? SELECT 2,'2005-01-21',N'成本',? 8
UNION? ALL????? SELECT 3,'2004-01-01',N'銷售',200
UNION? ALL????? SELECT 3,'2004-12-10',N'銷售', 80
UNION? ALL????? SELECT 3,'2005-01-15',N'銷售', 90
UNION? ALL????? SELECT 3,'2005-01-21',N'銷售',? 8
GO
--統(tǒng)計(jì)
DECLARE @Period char(6)
SET @Period='200501' --統(tǒng)計(jì)的年月
--統(tǒng)計(jì)處理
DECLARE @Last_Period char(6),@Previous_Period char(6)
SELECT @Last_Period=CONVERT(char(6),DATEADD(Year,-1,@Period+'01'),112),
?? ?@Previous_Period=CONVERT(char(6),DATEADD(Month,-1,@Period+'01'),112)
SELECT Dept,Expenses,Name,
?? ?C_Money,
?? ?L_Money,
?? ?L_UP=C_Money-L_Money,
?? ?L_Prec=CASE
?? ??? ??? ?WHEN L_Money=0 THEN '----'
?? ??? ??? ?ELSE SUBSTRING('↓-↑',CAST(SIGN(C_Money-L_Money) as int)+2,1)
?? ??? ??? ??? ?+CAST(CAST(ABS(C_Money-L_Money)*100/P_Money as decimal(10,2)) as varchar)+'%'
?? ??? ?END,
?? ?P_Money,
?? ?P_UP=C_Money-P_Money,
?? ?P_Prec=CASE
?? ??? ??? ?WHEN P_Money=0 THEN '----'
?? ??? ??? ?ELSE SUBSTRING('↓-↑',CAST(SIGN(C_Money-P_Money) as int)+2,1)
?? ??? ??? ??? ?+CAST(CAST(ABS(C_Money-P_Money)*100/P_Money as decimal(10,2)) as varchar)+'%'
?? ??? ?END
FROM(
?? ?SELECT a.Dept,b.Expenses,
?? ??? ?Name=CASE WHEN GROUPING(Name)=1 THEN '<合計(jì)>' ELSE a.Name END,?? ?
?? ??? ?C_Money=ISNULL(SUM(CASE CONVERT(char(6),b.Date,112) WHEN @Period THEN b.[Money] END),0),
?? ??? ?L_Money=ISNULL(SUM(CASE CONVERT(char(6),b.Date,112) WHEN @Last_Period THEN b.[Money] END),0),
?? ??? ?P_Money=ISNULL(SUM(CASE CONVERT(char(6),b.Date,112) WHEN @Previous_Period THEN b.[Money] END),0)
?? ?FROM Employee a,Expenses b
?? ?WHERE a.ID=b.EmployeeID
?? ??? ?AND CONVERT(char(6),b.Date,112) IN(@Last_Period,@Previous_Period,@Period)
?? ?GROUP BY a.Dept,b.Expenses,a.ID,a.Name WITH ROLLUP
?? ?HAVING (GROUPING(a.Name)=0 OR GROUPING(a.ID)=1)
?? ??? ?AND (GROUPING(a.ID)=0 OR GROUPING(b.Expenses)=0))a
/*--結(jié)果
Dept???? Expenses??? Name??? C_Money L_Money L_UP?? L_Prec??? P_Money P_UP?? P_Prec
------- --------- -------- --------- -------- -------- -------- -------- -------- ------
大客戶部?? 成本?? 張三?? ??? ?8.00? ??? ?.00????? 8.00????? ----?? ??? ? .00??? ??? ?8.00??? ??? ?----
大客戶部?? 成本?? 李四?? ??? ?8.00? ??? ?.00????? 8.00????? ----?? ??? ? 2.00?? ??? ?6.00??? ??? ?↑300.00%
大客戶部?? 成本?? <合計(jì)> ??? ?16.00 ??? ?.00????? 16.00???? ----? ??? ? 2.00?? ??? ?14.00? ??? ?↑700.00%
大客戶部?? 銷售?? 張三??? ??? ?170.00 ??? ?250.00? -80.00?? ↓32.00%?? ? 200.00 ??? ?-30.00 ??? ?↓15.00%
大客戶部?? 銷售?? 李四??? ??? ?50.00? ??? ?.00????? 50.00???? ----? ??? ? .00??? ??? ?50.00?? ??? ?----
大客戶部?? 銷售?? <合計(jì)>? ??? ?220.00 ??? ?250.00? -30.00?? ↓12.00% ?? ? 200.00 ??? ?20.00? ??? ?↑10.00%
銷售一部?? 銷售?? 王五??? ??? ?98.00? ??? ?200.00? -102.00? ↓51.00% ?? ? 80.00? ??? ?18.00? ??? ?↑22.50%
銷售一部?? 銷售?? <合計(jì)>? ??? ?98.00? ??? ?200.00? -102.00? ↓51.00% ?? ? 80.00? ??? ?18.00? ??? ?↑22.50%
--*/
CREATE TABLE Employee(
ID int,????????????? --雇員編號(hào)(主鍵)
Name nvarchar(10),?? --雇員名稱
Dept nvarchar(10))?? --所屬部門
INSERT Employee SELECT 1,N'張三',N'大客戶部'
UNION? ALL????? SELECT 2,N'李四',N'大客戶部'
UNION? ALL????? SELECT 3,N'王五',N'銷售一部'
--費(fèi)用表
CREATE TABLE Expenses(
EmployeeID int,??????? --雇員編號(hào)
Date Datetime,???????? --發(fā)生日期
Expenses nvarchar(10), --指標(biāo)名稱
[Money] decimal(10,2)) --發(fā)生金額
INSERT Expenses SELECT 1,'2004-01-01',N'銷售',100
UNION? ALL????? SELECT 1,'2004-01-02',N'銷售',150
UNION? ALL????? SELECT 1,'2004-12-01',N'銷售',200
UNION? ALL????? SELECT 1,'2005-01-10',N'銷售', 80
UNION? ALL????? SELECT 1,'2005-01-15',N'銷售', 90
UNION? ALL????? SELECT 1,'2005-01-21',N'成本',? 8
UNION? ALL????? SELECT 2,'2004-12-01',N'成本',? 2
UNION? ALL????? SELECT 2,'2005-01-10',N'銷售', 10
UNION? ALL????? SELECT 2,'2005-01-15',N'銷售', 40
UNION? ALL????? SELECT 2,'2005-01-21',N'成本',? 8
UNION? ALL????? SELECT 3,'2004-01-01',N'銷售',200
UNION? ALL????? SELECT 3,'2004-12-10',N'銷售', 80
UNION? ALL????? SELECT 3,'2005-01-15',N'銷售', 90
UNION? ALL????? SELECT 3,'2005-01-21',N'銷售',? 8
GO
--統(tǒng)計(jì)
DECLARE @Period char(6)
SET @Period='200501' --統(tǒng)計(jì)的年月
--統(tǒng)計(jì)處理
DECLARE @Last_Period char(6),@Previous_Period char(6)
SELECT @Last_Period=CONVERT(char(6),DATEADD(Year,-1,@Period+'01'),112),
?? ?@Previous_Period=CONVERT(char(6),DATEADD(Month,-1,@Period+'01'),112)
SELECT Dept,Expenses,Name,
?? ?C_Money,
?? ?L_Money,
?? ?L_UP=C_Money-L_Money,
?? ?L_Prec=CASE
?? ??? ??? ?WHEN L_Money=0 THEN '----'
?? ??? ??? ?ELSE SUBSTRING('↓-↑',CAST(SIGN(C_Money-L_Money) as int)+2,1)
?? ??? ??? ??? ?+CAST(CAST(ABS(C_Money-L_Money)*100/P_Money as decimal(10,2)) as varchar)+'%'
?? ??? ?END,
?? ?P_Money,
?? ?P_UP=C_Money-P_Money,
?? ?P_Prec=CASE
?? ??? ??? ?WHEN P_Money=0 THEN '----'
?? ??? ??? ?ELSE SUBSTRING('↓-↑',CAST(SIGN(C_Money-P_Money) as int)+2,1)
?? ??? ??? ??? ?+CAST(CAST(ABS(C_Money-P_Money)*100/P_Money as decimal(10,2)) as varchar)+'%'
?? ??? ?END
FROM(
?? ?SELECT a.Dept,b.Expenses,
?? ??? ?Name=CASE WHEN GROUPING(Name)=1 THEN '<合計(jì)>' ELSE a.Name END,?? ?
?? ??? ?C_Money=ISNULL(SUM(CASE CONVERT(char(6),b.Date,112) WHEN @Period THEN b.[Money] END),0),
?? ??? ?L_Money=ISNULL(SUM(CASE CONVERT(char(6),b.Date,112) WHEN @Last_Period THEN b.[Money] END),0),
?? ??? ?P_Money=ISNULL(SUM(CASE CONVERT(char(6),b.Date,112) WHEN @Previous_Period THEN b.[Money] END),0)
?? ?FROM Employee a,Expenses b
?? ?WHERE a.ID=b.EmployeeID
?? ??? ?AND CONVERT(char(6),b.Date,112) IN(@Last_Period,@Previous_Period,@Period)
?? ?GROUP BY a.Dept,b.Expenses,a.ID,a.Name WITH ROLLUP
?? ?HAVING (GROUPING(a.Name)=0 OR GROUPING(a.ID)=1)
?? ??? ?AND (GROUPING(a.ID)=0 OR GROUPING(b.Expenses)=0))a
/*--結(jié)果
Dept???? Expenses??? Name??? C_Money L_Money L_UP?? L_Prec??? P_Money P_UP?? P_Prec
------- --------- -------- --------- -------- -------- -------- -------- -------- ------
大客戶部?? 成本?? 張三?? ??? ?8.00? ??? ?.00????? 8.00????? ----?? ??? ? .00??? ??? ?8.00??? ??? ?----
大客戶部?? 成本?? 李四?? ??? ?8.00? ??? ?.00????? 8.00????? ----?? ??? ? 2.00?? ??? ?6.00??? ??? ?↑300.00%
大客戶部?? 成本?? <合計(jì)> ??? ?16.00 ??? ?.00????? 16.00???? ----? ??? ? 2.00?? ??? ?14.00? ??? ?↑700.00%
大客戶部?? 銷售?? 張三??? ??? ?170.00 ??? ?250.00? -80.00?? ↓32.00%?? ? 200.00 ??? ?-30.00 ??? ?↓15.00%
大客戶部?? 銷售?? 李四??? ??? ?50.00? ??? ?.00????? 50.00???? ----? ??? ? .00??? ??? ?50.00?? ??? ?----
大客戶部?? 銷售?? <合計(jì)>? ??? ?220.00 ??? ?250.00? -30.00?? ↓12.00% ?? ? 200.00 ??? ?20.00? ??? ?↑10.00%
銷售一部?? 銷售?? 王五??? ??? ?98.00? ??? ?200.00? -102.00? ↓51.00% ?? ? 80.00? ??? ?18.00? ??? ?↑22.50%
銷售一部?? 銷售?? <合計(jì)>? ??? ?98.00? ??? ?200.00? -102.00? ↓51.00% ?? ? 80.00? ??? ?18.00? ??? ?↑22.50%
--*/
轉(zhuǎn)載于:https://www.cnblogs.com/shihao/archive/2012/05/19/2508639.html
總結(jié)
以上是生活随笔為你收集整理的同期及上期数据对比处理示例.sql的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: static 用法www
- 下一篇: 一道概率算法