SQL 求中位值
题目
A median is defined as a number separating the higher half of a data set from the lower half.
Query the median of the Northern Latitudes (LAT_N) from STATION and round your answer to decimal places.
Input Format
The STATION table is described as follows:
Field type
ID Number
City varchar2(21)
state varchar2(2)
lat_n number
long_w number
where LAT_N is the northern latitude and LONG_W is the western longitude.
这是一道求中位值的题。
中位值是将所给的一组数从小到大或从大到小排列,奇数个数的话取中间的数字,偶数个数的话取中间两个数的平均数。
解答1
Select round(S.LAT_N,4) mediam from station S
where (select count(Lat_N) from station where Lat_N < S.LAT_N ) = (select count(Lat_N) from station where Lat_N > S.LAT_N)
解答2
select round(s.lat_n,4) from station s
where (select round(count(s.id)/2)-1 from station) = (select count(s1.id) from station s1 where s1.lat_n > s.lat_n);
解答3
SELECT
cast(
(SELECT MAX (lat_n) FROM
( SELECT TOP 50 PERCENT lat_n FROM station ORDER BY lat_n) AS H1)/2
+
( SELECT MIN (lat_n) FROM
( SELECT TOP 50 PERCENT lat_n FROM station ORDER BY lat_n DESC ) AS H2)/2
as numeric(21,4));
解答4
select cast(lat_n as decimal(10,4)) from
(select lat_n, row_number() over (order by lat_n desc) as rnum1 from station) t1
where rnum1 =
(select case when max(rnum)%2=0 then max(rnum)/2
else max(rnum)/2+1 end
from
(select row_number() over (order by lat_n desc) as rnum from station) t)
解答5
SET @N := 0;
SELECT COUNT(*) FROM STATION INTO @TOTAL;
SELECT
ROUND(AVG(A.LAT_N), 4)
FROM (SELECT @N := @N +1 AS ROW_ID, LAT_N FROM STATION ORDER BY LAT_N) A
WHERE
CASE WHEN MOD(@TOTAL, 2) = 0
THEN A.ROW_ID IN (@TOTAL/2, (@TOTAL/2+1))
ELSE A.ROW_ID = (@TOTAL+1)/2
END
;
解答6
set @rowindex := -1; /* 1) creates an index*/
/* 3) the outer query will select the average of the 2(for odd no. of values)/1(for even) values we found in the middle of the sorted array */
select round(avg(lat_n),4)
from
/* 2) the index will increment for each new value of lat_n it finds, and sort them by lat_n
*/
(select @rowindex:=@rowindex+1 as rowindex, lat_n
from station
order by lat_n) as l
where l.rowindex in (floor(@rowindex/2), ceil(@rowindex/2));
SQL 求中位值的更多相关文章
- 用SQL求1到N的质数和
今天在百度知道中,遇到了一位朋友求助:利用sql求1到1000的质数和.再说今天周五下午比较悠闲,我就在MSSQL 2008中写了出来,现在分享在博客中,下面直接贴代码: declare @num i ...
- sql求日期
2.求以下日期SQL: 昨天 select convert(varchar(10),getdate() - 1,120) 明天 select convert(varchar(10),getdate() ...
- sql求每家店铺销量前三的sku, 附python解法
背景 有一张表: date store_id sku sales 2023-01-01 CK005 03045 50 date 代表交易日期,store_id代表门店编号,sku代表商品,sales代 ...
- SQL求 交集 并集 差集
故事是这样的….. 故事情节: 表 tb_test 有两列, colA , colB; 求 colA , colB 的并交差集… -- 计算并集 SELECT DISTINCT colB FROM t ...
- SQL求差集
数据库环境:SQL SERVER 2008R2 Sql Server有提供求集合差集的函数——EXCEPT.先看看EXCEPT的用法, { <query_specification> | ...
- 分页过滤SQL求总条数SQL正则
public static void main(String[] args) throws Exception { String queryForScanUsers_SQL = "selec ...
- hive sql求多个字段的最小值和最大值的办法
1. 准备数据表test2 create table test2( a int, b int, c int, d int, e int); 2. 准备2条数据 ,,,,); ,,,,); 查询显示如下 ...
- SQL求出优秀、及格人数
首先看看班级的表的数据: 接下来,由于班级有分linux .Mysql.Java三门科目,因此,先求Linux科目的及格人数.不及格人数和优秀人数 做一个语句的分解: 1.首先查出每个班级的班级ID ...
- sql求倒数第二大的数,效率不高,但写法新颖
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- [oracle/sql]求员工表中每个部门里薪水最高的员工,那种sql最优?
开始正题前,先把我的数据库环境列出: # 类别 版本 1 操作系统 Win10 2 数据库 Oracle Database 11g Enterprise Edition Release 11.2.0. ...
随机推荐
- ABC195E
其实我们发现很多博弈论的动态规划都是从后往前的,比如过河卒和本题. 这是因为从某种角度上来说这些动态规划有后效性而无前效性. 所以设计状态 \(dp_{i,j}\) 表示第 \(i\) 次操作 \(T ...
- 如何合理开发Java接口(安全性,可重复调用,稳定性,追溯性)
一.接口开发规范 签名:对外提供的接口要做签名认证,认证不通过的请求不允许访问接口.提供服务. 加密:敏感数据在网络传输过程中应该加密. IP白名单:限制请求的IP,增加IP白名单,一般在网关层处理. ...
- 基于树莓派的OpenWrt系统打开蓝牙功能
在树莓派设备上的OpenWrt系统打开蓝牙功能 1. 安装必要的软件包 首先,你需要确保OpenWrt系统上安装了必要的蓝牙软件包.你可以通过OpenWrt的包管理器来安装它们.在OpenWrt系统上 ...
- vue项目坑记录:vue项目运行卡在百分之几几几
今天晚上打着游戏,同事突然叫我拉项目下来运行,我打完就去拉代码了,结果vue项目运行卡在66%不动了,我也是百度一下分享别人怎么解决的文章给他,继续我的游戏! 结果呢? 游戏结束后,我拉代码,还是这个 ...
- 从pytest源码的角度分析pytest工作原理
从pytest源码的角度分析pytest工作原理 从 pytest 源代码的角度来分析其工作原理,我们需要关注几个关键的部分,特别是 pytest 的启动过程以及测试的收集与执行.下面是基于 pyte ...
- telegraf 常用命令总结
本文为博主原创,转载请注明出处: Telegraf 是一个灵活的服务器代理,用于收集和报告指标.它支持插件驱动,这意味着你可以根据需要添加或修改功能. 1.使用telegraf --help 查看te ...
- 【ActiveJdbc】05
一.事务 通常在 Java ORM 中有一个显式连接或管理器对象(JPA 中的 EntityManager,Hibernate 中的 SessionManager 等). ActiveJDBC 中没有 ...
- Ubuntu-20.04.6-server安装MySQL实现远程连接
Ubuntu-20.04.6-server安装MySQL,修改密码 安装MySQL 一.查看是否安装数据库 mysql --version 二.更新系统中的所有软件包和存储库 sudo apt upd ...
- 【EF Core】自动生成的字段值
自动生成字段值,咱们首先想到的是主键列(带 IDENTITY 的主键).EF Core 默认的主键配置也是启用 Identity 自增长的,而且可以自动标识主键.前提是代表主键的实体属性名要符合以下规 ...
- 5分钟教你使用idea调试SeaTunnel自定义插件
在用Apache SeaTunnel研发SM2加密组件过程中,发现社区关于本地调试SeaTunnel文章过于简单,很多情况没有说明,于是根据自己遇到问题总结这篇文档.SeaTunnel本地调试官方文档 ...