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. ...
随机推荐
- P9212 题解
显然,我们维护的答案具有 可差分 性,所以转换为 \([1,r]\) 上的查询. 首先,对于 \(x,y,a_i\) 先对 \(m\) 取模不影响结果. 下面为了方便令 \(v = a_i\). 如果 ...
- P6655 题解
直接计数其实不好记,不如计数转期望. 令 \(f_i\) 表示点 \(i\) 成为制高点概率,不难发现期望就是 \(\sum f_i\). 根据定义对于 \(f\) 我们有如下转移 \(f_i = \ ...
- Ubuntu 查看用户历史记录
Ubuntu 查看用户历史记录 1. 查看用户命令行历史记录 1. 查看当前登录账号所属用户的历史命令行记录 打开命令行,输入 history 就会看到当前登录账号所属用户的历史记录 2. 查看系统所 ...
- Java 自定义注解校验字段唯一性
业务场景 在项目中,某些情景下我们需要验证编码是否重复,账号是否重复,身份证号是否重复等... 那么有没有办法可以解决这类似的重复代码量呢? 我们可以通过自定义注解校验的方式去实现,在实体类上面加上自 ...
- Java 对象转XML xStream 别名的使用 附下载方式
下载方式 Maven方式 pom.xml中 <dependency> <groupId>xstream</groupId> <artifactId>xs ...
- C#使用SendMessage进行进程间通讯
最近公司有个需求是,拖动文件到桌面图标上,自动打开文件.那么只需在OnStartup事件中通过StartupEventArgs获取文件名然后进行操作即可.操作之后发现当软件已经启动了(单例运行),那么 ...
- 4. 系统I/O
系统 I/O 示例代码: #include <iostream> // 标准库头文件 // #include "myheader.h" // 自己写的头文件 void ...
- 国产开源存储之光:Curve 通过信创认证
网易数帆喜讯再传,Curve 近日通过信息技术应用创新(信创)认证! Curve 是一款高性能.易运维.云原生的分布式存储系统,由网易数帆存储团队发起开源,现为 CNCF 沙箱项目.国家工业信息安全发 ...
- [oeasy]python0011_ 字符序号_ordinal_ord
序号(ordinal) 回忆上次内容 hello world 不是从来就有的 来自于unix和c 虽然我们今天有各种先进的学习手段 最早的高级语言学习是从最早的那张打字机用纸 ...
- odoo 为form表单视图添加chatter功能
实践环境 Odoo 14.0-20221212 (Community Edition) 需求描述 如图,给表单新增一个类似聊天的窗口,当记录一些表单活动(本例为自动记录当前记录状态变化) 需求实现 模 ...