SQL中位数函数实例
程序员文章站
2022-03-09 23:48:21
表还是total_sales添加一项
表:
sql语句:
复制代码 代码如下:select * from
(
selec...
表还是total_sales添加一项
表:
sql语句:
复制代码 代码如下:
select * from
(
select a1.name, a1.sales, count(a2.sales) sales_rank
from total_sales a1, total_sales a2
where a1.sales < a2.sales or (a1.sales=a2.sales and a1.name = a2.name)
group by a1.name, a1.sales
order by a1.sales desc, a1.name desc
) as tab where tab.sales_rank=(select (count(*)+1) div 2 from total_sales);
(
select a1.name, a1.sales, count(a2.sales) sales_rank
from total_sales a1, total_sales a2
where a1.sales < a2.sales or (a1.sales=a2.sales and a1.name = a2.name)
group by a1.name, a1.sales
order by a1.sales desc, a1.name desc
) as tab where tab.sales_rank=(select (count(*)+1) div 2 from total_sales);
可以查找到中间的项,count(*)+1,这个1必须要添加,总项数假如为奇数7,则count(*)+1 / 2 = 4,假如是6则为3,都是满足的,如果不+1的话,对奇数项则不满足,如7,count(*) /2 = 3,而我们要查找的则是位于4的位置
结果:
以上就是sql中位数函数实例的全部内容,希望能给大家一个参考,也希望大家多多支持。
上一篇: SQL查询数据库中符合条件的记录的总数