怎么计算这样的平均值?SQL语句能不能实现之?

bljbljbljblj 2003-09-15 10:27:31
怎么计算这样的平均值?
下面是例子

     茄子 白菜 萝卜 樱桃
第一季度 0 0.7 0.5 0
第二季度 1.8 0.3 0 2.0
第三季度 1.0 0.5 0 0
第四季度 0 0 0.6 0
-------------------------------------
我期望的
平均价 1.4 0.5 0.55 2.0
-------------------------------------
SQL句法
计算出来 0.7 0.375 0.275 0.05
的平均价

因为历史原因,把没有货的物品的价格定为0了,现在想计算均价的时候让价格为0的记录不参加计算,但是又不能把茄子 白菜 萝卜 樱桃分开来查询

请问SQL语句能不能做到这点?
谢谢
...全文
95 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
WQLu 2003-09-15
  • 打赏
  • 举报
回复
大哥,变一下就行了,我写代码只是一种思路:)
select * from (select avg(茄子) 茄子 from t where 茄子<>0) a,
,(select avg(白菜) 白菜 from t where 白菜<>0) b,
,(select avg(萝卜) 萝卜 from t where 萝卜<>0) c,
,(select avg(樱桃) 樱桃 from t where 樱桃<>0) d
bljbljbljblj 2003-09-15
  • 打赏
  • 举报
回复
佩服SI你们了,请去

http://expert.csdn.net/Expert/TopicView1.asp?id=2251219

看看,不会的帮忙顶顶帖子也行啊
wzh1215 2003-09-15
  • 打赏
  • 举报
回复
方法真多呀!
dafu71 2003-09-15
  • 打赏
  • 举报
回复
select avg(茄子), avg(白菜), avg(萝卜),avg(樱桃) from tablename
where 价格<>0 and 价格 is not null

WQLu 2003-09-15
  • 打赏
  • 举报
回复
select (select avg(茄子) 茄子 from t where 茄子<>0
,select avg(白菜) 白菜 from t where 白菜<>0
,select avg(萝卜) 萝卜 from t where 萝卜<>0
,select avg(樱桃) 樱桃 from t where 樱桃<>0
)
hot3000 2003-09-15
  • 打赏
  • 举报
回复
select avg(茄子), avg(白菜), avg(萝卜),avg(樱桃) from tablename
where (价格<>0) and not (价格 is null)
txlicenhe 2003-09-15
  • 打赏
  • 举报
回复
Select (case when sum(茄子) = 0 then 0 else sum(茄子)/sum(case when 茄子 = 0 then 0 else 1 end) end) as 茄子,
(case when sum(白菜) = 0 then 0 else sum(白菜)/sum(case when 白菜 = 0 then 0 else 1 end) end) as 白菜,
(case when sum(萝卜) = 0 then 0 else sum(萝卜)/sum(case when 萝卜 = 0 then 0 else 1 end) end) as 萝卜,
(case when sum(樱桃) = 0 then 0 else sum(樱桃)/sum(case when 樱桃 = 0 then 0 else 1 end) end) as 樱桃
from 表
yujohny 2003-09-15
  • 打赏
  • 举报
回复
select sum(茄子)/sum(case ISNULL(茄子,0) when 0 then 0 else 1 end),
sum(白菜)/sum(case ISNULL(白菜,0) when 0 then 0 else 1 end),
sum(萝卜)/sum(case ISNULL(萝卜,0) when 0 then 0 else 1 end),
sum(樱桃)/sum(case ISNULL(樱桃,0) when 0 then 0 else 1 end),
from 表
yujohny 2003-09-15
  • 打赏
  • 举报
回复
可以做到
用下面的语句
select sum(茄子)/sum(case ISNULL(茄子,0) when 0 then 0 else 1 end),
sum(白菜)/sum(case ISNULL(白菜,0) when 0 then 0 else 1 end),
sum(萝卜)/sum(case ISNULL(萝卜,0) when 0 then 0 else 1 end),
sum(樱桃)/sum(case ISNULL(樱桃,0) when 0 then 0 else 1 end),
from 表

34,576

社区成员

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

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