| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4009 人关注过本帖
标题:给位大佬帮帮忙呗,请帮我解答如何算出平均值
取消只看楼主 加入收藏
Melody6188
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2020-3-25
结帖率:0
收藏
已结贴  问题点数:20 回复次数:4 
给位大佬帮帮忙呗,请帮我解答如何算出平均值
下面是一组数据
SELECT '2019-01' AS Month, 'YT' AS BIZ, 'AP' AS Region, 100.90 AS Revenue
INTO T
UNION ALL
SELECT '2019-01' AS Month, 'ZY' AS BIZ, 'AP' AS Region, 200.90 AS Revenue
UNION ALL
SELECT '2019-01' AS Month, 'AIO' AS BIZ, 'AP' AS Region, 300 AS Revenue
UNION ALL
SELECT '2019-01' AS Month, 'XY' AS BIZ, 'AP' AS Region, 400 AS Revenue
UNION ALL
SELECT '2019-01' AS Month, 'AA' AS BIZ, 'AP' AS Region, 250 AS Revenue
UNION ALL
SELECT '2019-01' AS Month, 'BB' AS BIZ, 'AP' AS Region, 900 AS Revenue
UNION ALL
SELECT '2019-01' AS Month, 'XY' AS BIZ, 'EMEA' AS Region, 400 AS Revenue
UNION ALL
SELECT '2019-01' AS Month, 'AA' AS BIZ, 'EMEA' AS Region, 250 AS Revenue
UNION ALL
SELECT '2019-01' AS Month, 'BB' AS BIZ, 'EAEA' AS Region, 900 AS Revenue

UNION ALL
SELECT '2019-02' AS Month, 'AIO' AS BIZ, 'AP' AS Region, 300 AS Revenue
UNION ALL
SELECT '2019-02' AS Month, 'XY' AS BIZ, 'AP' AS Region, 400 AS Revenue
UNION ALL
SELECT '2019-02' AS Month, 'AA' AS BIZ, 'AP' AS Region, 250 AS Revenue
UNION ALL
SELECT '2019-02' AS Month, 'BB' AS BIZ, 'AP' AS Region, 900 AS Revenue

UNION ALL
SELECT '2019-02' AS Month, 'AIO' AS BIZ, 'EMEA' AS Region, 200 AS Revenue
UNION ALL
SELECT '2019-02' AS Month, 'XY' AS BIZ, 'EMEA' AS Region, 100 AS Revenue
UNION ALL
SELECT '2019-02' AS Month, 'AA' AS BIZ, 'EMEA' AS Region, 700 AS Revenue
UNION ALL
SELECT '2019-02' AS Month, 'BB' AS BIZ, 'EMEA' AS Region, 300 AS Revenue

UNION ALL
SELECT '2019-03' AS Month, 'AIO' AS BIZ, 'AP' AS Region, 300 AS Revenue
UNION ALL
SELECT '2019-03' AS Month, 'XY' AS BIZ, 'AP' AS Region, 400 AS Revenue
UNION ALL
SELECT '2019-03' AS Month, 'AA' AS BIZ, 'AP' AS Region, 250 AS Revenue
UNION ALL
SELECT '2019-03' AS Month, 'BB' AS BIZ, 'AP' AS Region, 900 AS Revenue

建表以后,
求月,地区,总值,平均值(相同区域跨月的平均)
下面的表格是期待的结果,数据略有出入,但大体的意思如图所示
谢谢
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: 平均值 SELECT 期待 数据 UNION 
2020-03-25 10:27
Melody6188
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2020-3-25
收藏
得分:0 
回复 2楼 xianfajushi
不是那么简单的,我已经试过了,那样是求不出来我要的结果的。
我要的结果是在group by 以后每个地区的平均值。
2020-03-26 06:27
Melody6188
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2020-3-25
收藏
得分:0 
回复 4楼 mywisdom88
谢谢您的回复,最后得到的平均值也不是我想要的结果。
我想要图中几个地区加起来以后的平均值,并不是原始表中的平均值。
比如现在的表里地区AP有三个,分别是2150,1850,1550,我想得到的平均值是他们三个加起来除以3以后的那个数字是1850
另外的两个地区也是这样的算法
是否考虑用到partition by这个function呢?
2020-03-28 07:15
Melody6188
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2020-3-25
收藏
得分:0 
回复 7楼 mywisdom88
谢谢您的建议,但是你的QUERY结果还是跟我想要的有区别
2020-03-31 10:27
Melody6188
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2020-3-25
收藏
得分:0 
回复 9楼 mywisdom88
我要的结果就是跟图里几乎是一摸一样的。尤其AP这个地区的值应该是跟图里一摸一样的,另外的两个地区略有区别
特别感谢您能一直回复我,因为我的问题还是没有解决
麻烦您费心
2020-04-01 07:05
快速回复:给位大佬帮帮忙呗,请帮我解答如何算出平均值
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021566 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved