3

我正在处理一个 T-SQL 查询(我正在运行一个 SQL Server 数据库),它应该从值列表中计算中位数。查询如下所示:

SELECT 
   PERCENTILE_CONT(0.5) OVER (ORDER BY age) as Age 
FROM 
   peopleDB 
WHERE 
   <expressions>;

数据库有 100 行。查询返回中位数(相同的值)100 次,但我的目标是只返回一次。我一直在尝试以多种方式使用DISTINCTGROUP BY,但每次都会返回语法错误。DISTINCT目前数据库不支持。

我有没有可能忽略一些明显的东西?

4

3 回答 3

0

尝试一个空的 over 子句。

SELECT 
   PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY <column>) OVER () as Age 
FROM 
   peopleDB 
WHERE 
   <expressions>;
于 2013-07-29T23:57:56.987 回答
0

我从来没有听说过TSL。但是,以下其中一项可能有效:

SELECT PERCENTILE_CONT(0.5) OVER (ORDER BY age) as Age
FROM peopleDB
WHERE <expressions>
limit 1;

SELECT top 1 PERCENTILE_CONT(0.5) OVER (ORDER BY age) as Age
FROM peopleDB
WHERE <expressions>;

SELECT max(PERCENTILE_CONT(0.5) OVER (ORDER BY age)) as Age
FROM peopleDB
WHERE <expressions>;

select max(age)
from (SELECT top 1 PERCENTILE_CONT(0.5) OVER (ORDER BY age) as Age
      FROM peopleDB
      WHERE <expressions>
     ) t
于 2013-06-25T01:59:59.350 回答
0

我遇到了同样的问题,阅读这有助于我实现解决方案。因此,您的问题的 SQL 语法(因为我不熟悉 TSQL)是:

SELECT 
   Distinct PERCENTILE_CONT(0.5) within group (ORDER BY age) over () as Age 
FROM 
   peopleDB 
WHERE 
   <expressions>;

为我工作!

于 2016-03-30T18:43:54.073 回答