日期:2014-05-18 浏览次数:20546 次
select 客户,日期,sum(价格),avg(均价) from 表一 where 日期>'2012-01-01' group by 客户,日期
------解决方案--------------------
if object_id('[TB]') is not null drop table [TB] go create table [TB] (价格 int,均价 int,客户 nvarchar(2),日期 datetime) insert into [TB] select 20,2,'b','2012/1/1' union all select 10,4,'b','2012/1/1' union all select 5,1,'c','2012/1/1' union all select 30,3,'a','2012/1/2' union all select 10,1,'a','2012/1/2' union all select 10,1,'c','2012/1/3' union all select 15,1,'a','2012/1/3' union all select 20,1,'a','2012/1/3' select * from [TB] SELECT CONVERT(VARCHAR(10),日期,120) AS 日期,客户,SUM(价格) AS 价格,AVG(均价) AS 均价 FROM dbo.tb GROUP BY CONVERT(VARCHAR(10),日期,120),客户 ORDER BY 日期,客户 /* 日期 客户 价格 均价 2012-01-01 b 30 3 2012-01-01 c 5 1 2012-01-02 a 40 2 2012-01-03 a 35 1 2012-01-03 c 10 1