Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 202966322B1 for ; Thu, 2 Jul 2009 18:21:26 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 52170-08 for ; Thu, 2 Jul 2009 18:21:18 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-bw0-f211.google.com (mail-bw0-f211.google.com [209.85.218.211]) by mail.postgresql.org (Postfix) with ESMTP id 198CF62F51F for ; Thu, 2 Jul 2009 18:21:22 -0300 (ADT) Received: by bwz7 with SMTP id 7so1975447bwz.19 for ; Thu, 02 Jul 2009 14:21:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:sender:received:in-reply-to :references:from:date:x-google-sender-auth:message-id:subject:to:cc :content-type:content-transfer-encoding; bh=31P3WjvtdfTIlWV3W6DRECYOhyJG1Q66iEtwe/Jd2wE=; b=pZqoTgWjaPuFS+K2S+9s0Q5FyV0k6tgaPtZxrvEdxouOoks3TrcwqVE7/yfBLGrv7w 3I2WgyOyuHnMnKI20Aos2VfrL5j8RKnJ0L0/yDrUYIk49ipW9YO4qwQ1HLuZSSo+ojqx NpVoWU+DCC1xUZxnkFwYQC4+RvPN9DE3GBsbA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; b=Mbg2g4FdnwAcd0Lxqzu0sxHxsN2Z9RcUPFIjZyx7Gb1UPb4zTkTDsIqxsce3vbkO7c WMpvkTa3C69vseP2x5NL7Fe0VweWdYQwo5s6lqhOneWc2qbjBHMgsfEJ22ji9mW88rYz qBeTKY0i+LxOnS2ReOKJrYQKWemnAhMparRHY= MIME-Version: 1.0 Received: by 10.103.6.14 with SMTP id j14mr290209mui.48.1246569681211; Thu, 02 Jul 2009 14:21:21 -0700 (PDT) In-Reply-To: References: From: Greg Stark Date: Thu, 2 Jul 2009 22:21:01 +0100 X-Google-Sender-Auth: e54f1f8959bc5904 Message-ID: <407d949e0907021421qb1df8b1o9468f343298f08dd@mail.gmail.com> Subject: Re: Create custom aggregate function and custom sfunc To: Jasmin Dizdarevic Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200907/12 X-Sequence-Number: 32753 On Thu, Jul 2, 2009 at 3:48 PM, Jasmin Dizdarevic 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=A02 : 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. --=20 greg http://mit.edu/~gsstark/resume.pdf