1

If I have a table

col1 |       name       | pay 
------+------------------+------
    1 | Steve Jobs       | 1006
    2 | Mike Markkula    | 1007
    3 | Mike Scott       | 1978
    4 | John Sculley     | 1983
    5 | Michael Spindler | 1653

The user executes a sum query which sums the pay of people getting paid more than $1500. Is there a way to also implicitly know which tuples have been used which satisfy the condition for sum ?

I know you can separately write another query to just return the primary key ids which satisfy the condition. But, Is there any other way to do that in the same query ? probably rewrite the query in some way ? or...

The basic idea is to store references to all tuples which have been used for aggregate queries.
any suggestion ?

4

1 回答 1

3

You can do that using array_agg

SELECT sum(pay), array_agg(col1)
FROM salaries
WHERE pay > 1500

array_agg aggregates values from column into array.

于 2013-10-26T16:22:39.630 回答