SQL中位数函数实例
时间:2021-04-16 08:25:55|栏目:MsSql|点击: 次
表还是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 Server无法收缩日志文件的原因分析及解决办法
栏 目:MsSql
本文标题:SQL中位数函数实例
本文地址:http://www.codeinn.net/misctech/102505.html