TSL / SQL PERCENTILE_CONT应返回1条记录

时间:2013-06-24 23:48:42

标签: sql sql-server group-by distinct-values percentile

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

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

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

我有没有机会忽视一些明显的东西?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

尝试一个空的over子句。

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

答案 2 :(得分:0)

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

UITableViewController

为我工作!