select a.销售时间,b.客户名称,sum(a.金额) 总金额
from TB_Order a left join TB_User b on a.客户ID=b.用户ID
--where a.销售时间='2016-05-01' --加上这个条件,就是查询某段时间的销售报表,现没加上这个条件
group by a.销售时间,b.客户名称 --分组汇总
order by a.销售时间,b.客户名称 --排序规则
with TB_Order
(客户ID , 销售时间 , 金额 ) as
(select 1 , '2016-05-01', 10 union all select
1, '2016-05-01', 15 union all select
2 , '2016-05-01', 20 union all select
1 , '2016-05-02', 30 union all select
1 , '2016-05-02', 35),
TB_User
(用户ID, 客户名称) as
(select 1 , '张三' union all select
2 , '李四')
select a.销售时间 as 日期 ,b.客户名称,SUM(a.金额) as 总金额 from TB_Order a,TB_User b where a.客户ID=b.用户ID
group by a.销售时间 ,b.客户名称 order by a.销售时间