表还是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中位数函数实例的全部内容,希望能给大家一个参考,也希望大家多多支持云图网。
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/235145.html