请教一个SUM的问题

发财蛇 2005-01-06 11:21:44
想统计出所有客户2005年的销售量,如果在2005年没有发生销售的为0
select sum(total) from cust where year(notedate) = '2005'
这样只能统计出2005年发生过销售的客户,无法将没发生销售的客户也列出来
请教下,这样的SQL语句该怎样写
...全文
199 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
发财蛇 2005-01-07
  • 打赏
  • 举报
回复
这样只能统计出2005年发生过销售的客户,无法将没发生销售的客户也列出来
例如,有些客户在2005年还没发生销售,这样就查询不出来这些客户的,我希望没发生销售的客户也列出来
NinGoo 2005-01-07
  • 打赏
  • 举报
回复
楼主给出的信息不全,呵呵

没发生销售的客户--这个信息你放在哪里?没有信息如何获取信息

客户表

销售表

然后按照上面的兄弟那样做left outer join,或者用union也行
lxysjl 2005-01-07
  • 打赏
  • 举报
回复
select 客户名 from 客户表 as a left join (select 客户名,sum(total) from cust where year(notedate) = '2005' group by 客户名 ) as b on a.客户名=b.客户名
老宛 2005-01-07
  • 打赏
  • 举报
回复
1楼说的不错,要有一个客户表,再用left join
zhuxiaojun2002 2005-01-07
  • 打赏
  • 举报
回复
想统计出所有客户2005年的销售量,如果在2005年没有发生销售的为0
select sum(total) from cust where year(notedate) = '2005'
这样只能统计出2005年发生过销售的客户,无法将没发生销售的客户也列出来

2005年没有发生销售的为0,你求的是所有客户2005年的销售量。0统计出来加到sum中有什么用啊?
xluzhong 2005-01-07
  • 打赏
  • 举报
回复
select distinct 客户 into #t from cust

select #t.*,sum(cust.total)
from #t
left join cust
on #t.客户=cust.客户
where year(notedate) = '2005'
long_205 2005-01-07
  • 打赏
  • 举报
回复
要有一个客户表,列出所有客户,下面假定它的名称是c,另外cust中的FK是customname:
select c.customname,isnull(sum(cust.total),0)
from c left join cust on cust.customname = c.customname
where year(cust.notedate)='2005'
group by c.customname

如果没有客户表,客户名称再cust里面:
select a.customname,isnull(sum(cust.total),0)
from (select dictinct customname from cust) a left join cust on cust.customname=a.customname
where year(cust.notedate)='2005'
group by a.customname

nicebee 2005-01-06
  • 打赏
  • 举报
回复
如果是这个意思的话,可能你是想列出不同客户的销售总量吧?
select custname,sum(money) from cust
where year(notedate)='2005'
group by custname

这样你可以看出每个客户的销售总量了。
canyqf 2005-01-06
  • 打赏
  • 举报
回复
上面写错了一个:dictinct 应为 distinct
canyqf 2005-01-06
  • 打赏
  • 举报
回复
要有一个客户表,列出所有客户,下面假定它的名称是c,另外cust中的FK是customname:
select c.customname,isnull(sum(cust.total),0)
from c left join cust on cust.customname = c.customname
where year(cust.notedate)='2005'
group by c.customname

如果没有客户表,客户名称再cust里面:
select a.customname,isnull(sum(cust.total),0)
from (select dictinct customname from cust) a left join cust on cust.customname=a.customname
where year(cust.notedate)='2005'
group by a.customname

34,590

社区成员

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

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