pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Greg Stark <gsstark@mit.edu>
To: Jasmin Dizdarevic <jasmin.dizdarevic@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Create custom aggregate function and custom sfunc
Date: Thu, 2 Jul 2009 22:21:01 +0100
Message-ID: <407d949e0907021421qb1df8b1o9468f343298f08dd@mail.gmail.com> (raw)
In-Reply-To: <a0eee4d40907020748p7cb9bad7mad8817e60e9ea89@mail.gmail.com>
References: <a0eee4d40907020748p7cb9bad7mad8817e60e9ea89@mail.gmail.com>

On Thu, Jul 2, 2009 at 3:48 PM, Jasmin
Dizdarevic<jasmin.dizdarevic@gmail.com> wrote:
> customer ; seg
> 111 ; L1
> 111 ; L2
> 111 ; L1
> 222 ; L3
> 222 ; L3
> 222 ; L2
>
> the result should look like this:
>
> 111: L1 - because L1 is higher than L2 and ratio of L1 : L2 is 2 : 1
> 222: L3 - because L3 is higher than L2 and ratio of L3 : L2 isĀ 2 : 1

Are you just looking for the most frequent seg for each customer?

select distinct on (customer) customer,seg
   from (select customer, seg, count(*) as n from tab group by seg)
  order by customer, n desc

That doesn't give the ratios though.

-- 
greg
http://mit.edu/~gsstark/resume.pdf



view thread (5+ messages)  latest in thread

Message-ID: <407d949e0907021421qb1df8b1o9468f343298f08dd@mail.gmail.com>
Permalink:  ../407d949e0907021421qb1df8b1o9468f343298f08dd@mail.gmail.com/
Also on:    postgresql.org/message-id/407d949e0907021421qb1df8b1o9468f343298f08dd@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: gsstark@mit.edu, jasmin.dizdarevic@gmail.com
  Subject: Re: Create custom aggregate function and custom sfunc
  In-Reply-To: <407d949e0907021421qb1df8b1o9468f343298f08dd@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox