sql语句怎么实现聚集函数嵌套

有时辰我们在写sql语句的时辰,想实现堆积函数嵌套,怎么实现呢,下面来分享一下方式

需要这些哦
sql语句
实现堆积函数嵌套方式
方式/
1第一步我们在写sql语句的时辰,写sum(min(a))这种函数嵌套格局会呈现语法错误,下面以test表为例,来实现堆积函数嵌套,如下图所示:

sql语句怎么实现聚集函数嵌套

文章插图

2第二步输入“select sum(diff) from (SELECT max(second)-min(second) as diff from test GROUP BY name) as a”语句,如许就实现了sum函数和max函数、min函数的嵌套,这句sql语句的意思是按name字段进行分组,并求出每组的最年夜值和最小值之差,对所有组的差值进行乞降,如下图所示:

sql语句怎么实现聚集函数嵌套

文章插图

3第三步运行sql语句,我们可以看到成果为38,张三的最年夜最小差值为34,李四的为4,王五的为0,加起来成果准确,如下图所示:

sql语句怎么实现聚集函数嵌套

文章插图

4第四步我们也可以输入“select avg(diff) from (SELECT max(second)-min(second) as diff from test GROUP BY name) as a”语句,计较每组最年夜值和最小值之差的平均值,成果为12.6667,如下图所示:

sql语句怎么实现聚集函数嵌套

文章插图

5第五步输入“select count(diff) from (SELECT max(second)-min(second) as diff from test GROUP BY name) as a”语句,计较分了几组,如下图所示:

sql语句怎么实现聚集函数嵌套

文章插图

6【sql语句怎么实现聚集函数嵌套】第六步输入“select avg(diff) from (SELECT max(second) as diff from test GROUP BY name) as a”语句,计较每组的最年夜值平均值,如下图所示:

sql语句怎么实现聚集函数嵌套

文章插图

注重事项若是你感觉这篇对你有效的话,请投一下票

以上内容就是sql语句怎么实现聚集函数嵌套的内容啦,希望对你有所帮助哦!

    推荐阅读