原数据
年份 月份 销售日期 客户 产品 金额
2013 12 2013-12-1 A1 P1 100
2013 12 2013-12-25 A1 P1 200
2013 12 2013-12-5 A2 P2 150
2014 1 2014-1-2 A1 P1 1000
2014 1 2014-1-23 A1 P1 2000
2014 2 2014-2-15 A1 P1 1500
求一SQL语句,得出如下结果
年份 月份 销售日期 客户 产品 按年月日累计金额
2013 12 2013-12-1 A1 P1 100
2013 12 2013-12-25 A1 P1 300
2013 12 2013-12-5 A2 P2 150
2014 1 2014-1-2 A1 P1 1000
2014 1 2014-1-23 A1 P1 3000
2014 2 2014-2-15 A1 P1 1500
5 个解决方案
#1
按年份,月份, 产品分组排序(rownumber over partition by),再递归计算分组后数据的和,就行了
#2
select
年份,月份,销售日期,客户,产品,
(select sum(金额) as 按年月日累计金额 from tb where 年份=t.年份 and 月份=t.月份 and 客户=t.客户 and 产品=t.产品 and 销售日期<t.销售日期)
from
tb t
#3
if object_id('[TBA]') is not null drop table [TBA]
create table [TBA] (年份 int,月份 int,销售日期 datetime,客户 varchar(2),产品 varchar(2),金额 int)
insert into [TBA]
select 2013,12,'2013-12-1','A1','P1',100 union all
select 2013,12,'2013-12-25','A1','P1',200 union all
select 2013,12,'2013-12-5','A2','P2',150 union all
select 2014,1,'2014-1-2','A1','P1',1000 union all
select 2014,1,'2014-1-23','A1','P1',2000 union all
select 2014,2,'2014-2-15','A1','P1',1500
select * from [TBA]
SELECT A.年份,A.月份,CONVERT(VARCHAR(10),A.销售日期,120) AS 销售日期,A.客户,A.产品,SUM(B.金额) AS 金额
FROM TBA A
INNER JOIN TBA B ON A.年份 = B.年份 AND A.月份 = B.月份 AND A.销售日期>=B.销售日期 AND A.客户 = B.客户 AND A.产品 = B.产品
GROUP BY A.年份,A.月份,CONVERT(VARCHAR(10),A.销售日期,120) ,A.客户,A.产品
ORDER BY A.客户,A.产品
/*
年份 月份 销售日期 客户 产品 金额
2013 12 2013-12-01 A1 P1 100
2013 12 2013-12-25 A1 P1 300
2014 1 2014-01-02 A1 P1 1000
2014 1 2014-01-23 A1 P1 3000
2014 2 2014-02-15 A1 P1 1500
2013 12 2013-12-05 A2 P2 150*/
#4
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-06-06 11:11:16
-- Version:
-- Microsoft SQL Server 2012 - 11.0.2100.60 (Intel X86)
-- Feb 10 2012 19:13:17
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([年份] int,[月份] int,[销售日期] datetime,[客户] varchar(2),[产品] varchar(2),[金额] int)
insert [tb]
select 2013,12,'2013-12-1','A1','P1',100 union all
select 2013,12,'2013-12-25','A1','P1',200 union all
select 2013,12,'2013-12-5','A2','P2',150 union all
select 2014,1,'2014-1-2','A1','P1',1000 union all
select 2014,1,'2014-1-23','A1','P1',2000 union all
select 2014,2,'2014-2-15','A1','P1',1500
--------------开始查询--------------------------
select
年份,月份,销售日期,客户,产品,
(select sum(金额) as 按年月日累计金额 from tb where 年份=t.年份 and 月份=t.月份 and 客户=t.客户 and 产品=t.产品 and 销售日期<=t.销售日期) as 按年月日累计金额
from
tb t
----------------结果----------------------------
/* 年份 月份 销售日期 客户 产品 按年月日累计金额
----------- ----------- ----------------------- ---- ---- -----------
2013 12 2013-12-01 00:00:00.000 A1 P1 100
2013 12 2013-12-25 00:00:00.000 A1 P1 300
2013 12 2013-12-05 00:00:00.000 A2 P2 150
2014 1 2014-01-02 00:00:00.000 A1 P1 1000
2014 1 2014-01-23 00:00:00.000 A1 P1 3000
2014 2 2014-02-15 00:00:00.000 A1 P1 1500
(6 行受影响)
*/
掉了一个=号
#5
顶下小F
#1
按年份,月份, 产品分组排序(rownumber over partition by),再递归计算分组后数据的和,就行了
#2
select
年份,月份,销售日期,客户,产品,
(select sum(金额) as 按年月日累计金额 from tb where 年份=t.年份 and 月份=t.月份 and 客户=t.客户 and 产品=t.产品 and 销售日期<t.销售日期)
from
tb t
#3
if object_id('[TBA]') is not null drop table [TBA]
create table [TBA] (年份 int,月份 int,销售日期 datetime,客户 varchar(2),产品 varchar(2),金额 int)
insert into [TBA]
select 2013,12,'2013-12-1','A1','P1',100 union all
select 2013,12,'2013-12-25','A1','P1',200 union all
select 2013,12,'2013-12-5','A2','P2',150 union all
select 2014,1,'2014-1-2','A1','P1',1000 union all
select 2014,1,'2014-1-23','A1','P1',2000 union all
select 2014,2,'2014-2-15','A1','P1',1500
select * from [TBA]
SELECT A.年份,A.月份,CONVERT(VARCHAR(10),A.销售日期,120) AS 销售日期,A.客户,A.产品,SUM(B.金额) AS 金额
FROM TBA A
INNER JOIN TBA B ON A.年份 = B.年份 AND A.月份 = B.月份 AND A.销售日期>=B.销售日期 AND A.客户 = B.客户 AND A.产品 = B.产品
GROUP BY A.年份,A.月份,CONVERT(VARCHAR(10),A.销售日期,120) ,A.客户,A.产品
ORDER BY A.客户,A.产品
/*
年份 月份 销售日期 客户 产品 金额
2013 12 2013-12-01 A1 P1 100
2013 12 2013-12-25 A1 P1 300
2014 1 2014-01-02 A1 P1 1000
2014 1 2014-01-23 A1 P1 3000
2014 2 2014-02-15 A1 P1 1500
2013 12 2013-12-05 A2 P2 150*/
#4
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2014-06-06 11:11:16
-- Version:
-- Microsoft SQL Server 2012 - 11.0.2100.60 (Intel X86)
-- Feb 10 2012 19:13:17
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition: Core-based Licensing on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([年份] int,[月份] int,[销售日期] datetime,[客户] varchar(2),[产品] varchar(2),[金额] int)
insert [tb]
select 2013,12,'2013-12-1','A1','P1',100 union all
select 2013,12,'2013-12-25','A1','P1',200 union all
select 2013,12,'2013-12-5','A2','P2',150 union all
select 2014,1,'2014-1-2','A1','P1',1000 union all
select 2014,1,'2014-1-23','A1','P1',2000 union all
select 2014,2,'2014-2-15','A1','P1',1500
--------------开始查询--------------------------
select
年份,月份,销售日期,客户,产品,
(select sum(金额) as 按年月日累计金额 from tb where 年份=t.年份 and 月份=t.月份 and 客户=t.客户 and 产品=t.产品 and 销售日期<=t.销售日期) as 按年月日累计金额
from
tb t
----------------结果----------------------------
/* 年份 月份 销售日期 客户 产品 按年月日累计金额
----------- ----------- ----------------------- ---- ---- -----------
2013 12 2013-12-01 00:00:00.000 A1 P1 100
2013 12 2013-12-25 00:00:00.000 A1 P1 300
2013 12 2013-12-05 00:00:00.000 A2 P2 150
2014 1 2014-01-02 00:00:00.000 A1 P1 1000
2014 1 2014-01-23 00:00:00.000 A1 P1 3000
2014 2 2014-02-15 00:00:00.000 A1 P1 1500
(6 行受影响)
*/
掉了一个=号
#5
顶下小F