I'm looking to pull the top 10% of a summed value on a Postgres sever.
So i'm summing a value with sum(transaction.value) and i'd like the top 10% of the value
I'm looking to pull the top 10% of a summed value on a Postgres sever.
So i'm summing a value with sum(transaction.value) and i'd like the top 10% of the value
 
    
     
    
    From what I gather in your comments, I assume you want to:
WITH cte AS (
   SELECT t.customer_id, sum(t.value) AS sum_value
   FROM   transaction t
   GROUP  BY 1
   )
SELECT *, rank() OVER (ORDER BY sum_value DESC) AS sails_rank
FROM   cte
ORDER  BY sum_value DESC
LIMIT (SELECT count(*)/10 FROM cte)
Best to use a CTE here, makes the count cheaper.
The JOIN between customer and transaction automatically excludes customers without transaction. I am assuming relational integrity here (fk constraint on customer_id).
Dividing bigint / int effectively truncates the result (round down to the nearest integer). You may be interested in this related question:
PostgreSQL equivalent for TOP n WITH TIES: LIMIT "with ties"?
I added a sails_rank column which you didn't ask for, but seems to fit your requirement.
As you can see, I didn't even include the table customer in the query. Assuming you have a foreign key constraint on customer_id, that would be redundant (and slower). If you want additional columns from customer in the result, join customer to the result of above query:
WITH cte AS (
   SELECT t.customer_id, sum(t.value) AS sum_value
   FROM   transaction t
   GROUP  BY 1
   )
SELECT c.customer_id, c.name, sub.sum_value, sub.sails_rank
FROM  (
   SELECT *, rank() OVER (ORDER BY sum_value DESC) AS sails_rank
   FROM   cte
   ORDER  BY sum_value DESC
   LIMIT (SELECT count(*)/10 FROM cte)
   ) sub
JOIN  customer c USING (customer_id);
 
    
    