请教一个SQL问题

zoouii 2007-06-14 12:15:00
有两张表:
客户表customer:
id 客户编号(主键)
name 客户名称

销售数据表sell:
id 销售单号
customer_id 客户编号(外键)
name 商品名称
count 商品数量
price 商品价格
total 商品总价格(count*price得到)
date 销售时间

一个客户可以有多条销售记录,现在要查询一个时间段内的销售记录,产生如下的查询结果,请问SQL如何写:

客户编号§客户名称§销售记录总数量§销售总价格
...全文
198 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ojuju10 2007-06-14
  • 打赏
  • 举报
回复
select a.id as 客户编号, a.name as 客户名称, (select sum([count]) from sell where b.id=id ) as 销售记录总数量,(select sum([total]) from sell where b.id=id ) as 销售总价格 from customer a,sell b
where a.id=b.customer_id and date between @begintime and @endtime
group by a.id,a.name
ojuju10 2007-06-14
  • 打赏
  • 举报
回复

select a.id as 客户编号, a.name as 客户名称, (select sum([count]) from sell where b.id=id ) as 销售记录总数量,(select sum([total]) from sell where b.id=id ) as 销售总价格 from customer a,sell b
where a.id=b.customer_id
group by a.id,a.name
hellowork 2007-06-14
  • 打赏
  • 举报
回复
select a.客户编号,a.客户名称,
总数量 = sum(b.[count]),
总价格 = sum(b.[count]*b.price)
from customer as a left join sell as b on a.id = b.customerID
WHERE [date] between @起始日期 and @截止日期 /*指定时间段*/
group by a.客户编号,a.客户名称
lllyyymmm 2007-06-14
  • 打赏
  • 举报
回复
select a.客户编号,a.客户名称,
总数量 = sum(b.[count]),
总价格 = sum(b.[count]*b.price)
from customer as a left join sell as b on a.id = b.customerID
WHERE [date] >= @起始日期 and [date]<=@截止日期
group by a.客户编号,a.客户名称
y_dong119 2007-06-14
  • 打赏
  • 举报
回复
恩,都很好,接分

34,571

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧