题目
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 求中位值的更多相关文章

  1. 用SQL求1到N的质数和

    今天在百度知道中,遇到了一位朋友求助:利用sql求1到1000的质数和.再说今天周五下午比较悠闲,我就在MSSQL 2008中写了出来,现在分享在博客中,下面直接贴代码: declare @num i ...

  2. sql求日期

    2.求以下日期SQL: 昨天 select convert(varchar(10),getdate() - 1,120) 明天 select convert(varchar(10),getdate() ...

  3. sql求每家店铺销量前三的sku, 附python解法

    背景 有一张表: date store_id sku sales 2023-01-01 CK005 03045 50 date 代表交易日期,store_id代表门店编号,sku代表商品,sales代 ...

  4. SQL求 交集 并集 差集

    故事是这样的….. 故事情节: 表 tb_test 有两列, colA , colB; 求 colA , colB 的并交差集… -- 计算并集 SELECT DISTINCT colB FROM t ...

  5. SQL求差集

    数据库环境:SQL SERVER 2008R2 Sql Server有提供求集合差集的函数——EXCEPT.先看看EXCEPT的用法, { <query_specification> | ...

  6. 分页过滤SQL求总条数SQL正则

    public static void main(String[] args) throws Exception { String queryForScanUsers_SQL = "selec ...

  7. hive sql求多个字段的最小值和最大值的办法

    1. 准备数据表test2 create table test2( a int, b int, c int, d int, e int); 2. 准备2条数据 ,,,,); ,,,,); 查询显示如下 ...

  8. SQL求出优秀、及格人数

    首先看看班级的表的数据: 接下来,由于班级有分linux .Mysql.Java三门科目,因此,先求Linux科目的及格人数.不及格人数和优秀人数 做一个语句的分解: 1.首先查出每个班级的班级ID ...

  9. sql求倒数第二大的数,效率不高,但写法新颖

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. [oracle/sql]求员工表中每个部门里薪水最高的员工,那种sql最优?

    开始正题前,先把我的数据库环境列出: # 类别 版本 1 操作系统 Win10 2 数据库 Oracle Database 11g Enterprise Edition Release 11.2.0. ...

随机推荐

  1. 7.条件渲染if

    v-if v-if 指令用于条件性地渲染一块内容.这块内容只会在指令的表达式返回 truthy 值的时候被渲染. <h1 v-if="awesome">Vue is a ...

  2. 如何优雅地使用Mybatis逆向工程生成类

    文/朱季谦 1.环境:SpringBoot 2.在pom.xml文件里引入相关依赖: 1 <plugin> 2 <groupId>org.mybatis.generator&l ...

  3. Java-EL表达式替换和简化jsp页面中java代码的编写

    概念:Expression Language 表达式语言 作用:替换和简化jsp页面中java代码的编写 语法:$ 注意: jsp默认支持el表达式,如果要忽略el表达式 设置jsp中page指令中: ...

  4. HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`

    WARNINGS: customers.PackingHead.packing_date: (fields.W161) Fixed default value provided. HINT: It s ...

  5. [oeasy]python0141_自制模块_module_reusability_复用性

    自制包内容 回忆上次内容 上次导入了外部的py文件 import my_module 导入一个自己定义的模块   可以使用my_module中的变量 不能 直接使用 my_module.py文件中的变 ...

  6. Volatile不保证原子性及解决方案

    原子性的意义 原子性特别是在并发编程领域,是一个极其重要的概念,原子性指的是一个操作或一组操作要么全部执行成功,要么全部不执行,不会出现部分执行的情况.这意味着原子性操作是不可分割的,它们在执行过程中 ...

  7. 【JavaScript高级02】JavaScript第一大神兽:原型和原型链

    1,函数中的prototype属性 每个函数都会有一个属性prototy,该属性默认指向一个空Object对象,而这个空的Object对象被称之为原型对象. <script > conso ...

  8. httpURLConnection 请求发起post请求

    常见请求头,在post请求之 前先了解一下,请求相关的基础 关于post 请求的方式比get  多了很多配置,其实大致一样,本想将get示例和post写在一起,这个博客功能有时有问题 一直在灰色的编辑 ...

  9. springboot集成minIO

    文件系统:负责管理和存储文件的系统软件.操作系统通过文件系统提供的接口去存取文件,用户通过操作系统访问磁盘上的文件 minIO:轻量级服务分布式文件系统,适合存储非机构化数据.采用去中心化共享架构,结 ...

  10. Umov移动方块-scratch编程作品

    程序说明: <Umov移动方块>是一款基于Scratch平台制作的小游戏.在这个游戏中,玩家将面对一个3×3的圆圈棋盘,并通过鼠标控制蓝色方块在这些圆圈中灵活移动.游戏的挑战在于,舞台的四 ...