在复杂查询上生成有序序列等级

时间:2014-09-08 20:06:09

标签: postgresql subquery

我正在尝试在查询的输出中创建隐式排名。问题似乎是row_number()在计算worth之前运行。

SELECT 
    firstname,
    lastname,
    personid,
    year,
    (
        SELECT COALESCE(SUM(thevalue),0) 
        FROM assets WHERE personidref = w.personid AND year = w.year
    ) AS assets ,
    (
        SELECT COALESCE(SUM(amount),0) 
        FROM liabilities WHERE personidref = w.personid AND year = w.year
    ) AS liabilities,
    (
        (SELECT COALESCE(SUM(thevalue),0) 
         FROM assets 
         WHERE personidref = w.personid AND year = w.year) 
        - 
        (SELECT COALESCE(SUM(amount),0) 
         FROM liabilities 
         WHERE personidref = w.personid AND year = w.year)
    ) as worth,
    row_number() over(ORDER BY w.worth) as rank 
FROM members w 
WHERE year = 2012 
ORDER BY worth DESC LIMIT 2;

结果是我得到了这个:

| firstname | lastname | personid | year | assets | liabilities | worth | rank |
+-----------+----------+----------+------+--------+-------------+-------+------+
| foo       | bar      | 234      | 2012 | 30000  | 20          | 29980 | 32   |
| foo2      | bar2     | 5234     | 2012 | 30000  | 100         | 29900 | 69   |

而不是这个期望的输出:

| firstname | lastname | personid | year | assets | liabilities | worth | rank |
+-----------+----------+----------+------+--------+-------------+-------+------+
| foo       | bar      | 234      | 2012 | 30000  | 20          | 29980 | 1    |
| foo2      | bar2     | 5234     | 2012 | 30000  | 100         | 29900 | 2    |

有没有办法预先运行此查询并按排名先排序?

2 个答案:

答案 0 :(得分:1)

如何在row_number()上加倍?

with enchilada as (
SELECT firstname,lastname,personid,year,(SELECT COALESCE(SUM(thevalue),0)
FROM assets WHERE personidref = w.personid) AS assets ,
(SELECT COALESCE(SUM(amount),0) FROM liabilities WHERE personidref = w.personid AND year = w.year) AS liabilities,
((SELECT COALESCE(SUM(thevalue),0) FROM assets WHERE personidref = w.personid AND year = w.year) - (SELECT COALESCE(SUM(amount),0) FROM liabilities WHERE personidref = w.personid AND year = w.year)) as worth,
row_number() over(ORDER BY w.worth) as rank
FROM members w
WHERE year = 2012
ORDER BY worth DESC LIMIT 2 )
select row_number() over (order by rank) as new_rank, * from enchilada;

答案 1 :(得分:1)

如果没有样本数据,很难确定,但如果您想要排名,why not use rank or dense_rank

rank() over(ORDER BY w.worth) as rank 

dense_rank() over(ORDER BY w.worth) as rank 

以下是我认为你可能会尝试做的事情。未经测试,因为没有样本数据或http://sqlfiddle.com/要测试,但是:

SELECT
    *,
    dense_rank() OVER (ORDER BY worth)
FROM
(
    SELECT
        *,
        assets - liabilities AS worth
    FROM
    (
        SELECT 
            firstname,
            lastname,
            personid,
            year,
            (
                SELECT COALESCE(SUM(a.thevalue),0) 
                FROM assets a WHERE a.personidref = w.personid AND a.year = w.year
            ) AS assets ,
            (
                SELECT COALESCE(SUM(l.amount),0) 
                FROM liabilities l WHERE l.personidref = w.personid AND l.year = w.year
            ) AS liabilities
        FROM members w
        WHERE year = 2012
    ) AS a_and_l
) AS net_worths
ORDER BY worth DESC
LIMIT 2;