如何 select 最高值并从 Postgresql 嵌套查询中减去较低的值
How to select the highest value and subtract the lower values from Postgresql nested query
这就是我想要实现的目标 - 例如,假设我有一个包含人们年龄的整数列,我想 select 最高年龄并得到它与其他年龄的差异 select编辑年龄。所以如果我这样做:
SELECT ages FROM people
ORDER BY ages DESC
并得到 30 25 20 15 10
例如,我想做另一个 SELECT
来执行 30-30, 30-25, 30-20, 30-15, 30-10
SELECT --how do I perform that here?
FROM (
SELECT ages FROM peoeple
ORDER BY ages DESC
)foo
我怎样才能做到这一点?
使用 returns 最大年龄的子查询:
select (select max(age) from people) - age
from people
order by 1 -- ordering is optional, but it seems you want data in this order
顺便说一句,age
比 ages
更适合某人的年龄。
这就是我想要实现的目标 - 例如,假设我有一个包含人们年龄的整数列,我想 select 最高年龄并得到它与其他年龄的差异 select编辑年龄。所以如果我这样做:
SELECT ages FROM people
ORDER BY ages DESC
并得到 30 25 20 15 10
例如,我想做另一个 SELECT
来执行 30-30, 30-25, 30-20, 30-15, 30-10
SELECT --how do I perform that here?
FROM (
SELECT ages FROM peoeple
ORDER BY ages DESC
)foo
我怎样才能做到这一点?
使用 returns 最大年龄的子查询:
select (select max(age) from people) - age
from people
order by 1 -- ordering is optional, but it seems you want data in this order
顺便说一句,age
比 ages
更适合某人的年龄。