Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bcEKc-0002JO-Oj for pgsql-sql@arkaria.postgresql.org; Tue, 23 Aug 2016 16:13:42 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bcEKb-0007zi-Nj for pgsql-sql@arkaria.postgresql.org; Tue, 23 Aug 2016 16:13:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bcEKa-0007zT-Vd for pgsql-sql@postgresql.org; Tue, 23 Aug 2016 16:13:41 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bcEKX-0005qD-6w for pgsql-sql@postgresql.org; Tue, 23 Aug 2016 16:13:40 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 6A9FC205ED for ; Tue, 23 Aug 2016 12:13:35 -0400 (EDT) Received: from web3 ([10.202.2.213]) by compute1.internal (MEProxy); Tue, 23 Aug 2016 12:13:35 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.fm; h= content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to:x-sasl-enc:x-sasl-enc; s=mesmtp; bh=VOa WxTO0bYpoxM9SLcmyLERr+P4=; b=lBqprKbFPhVTyysqMBq5HccQVsAV4WvLWBp QaUskiBhjL23D4449VML8pULSpfnvZ7APBbbVKOHDpMZlFQpG/THan/qWOmkzGEf D/QSmqkNzcgEIutpD9Z0nhP4J9rBU6bNX5CJY/PhuTbT3NS9Ok0HdhfsYp9tyAVx ZBAmZsIY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:message-id:mime-version:subject:to:x-sasl-enc :x-sasl-enc; s=smtpout; bh=VOaWxTO0bYpoxM9SLcmyLERr+P4=; b=uXNsM sWdqLrORQQqZOJRG0owGT7QvUdSJJqDtMlSf8wO8u+H5MbF4NQIG0BcXiOUmFDPH EmURTAmWC/GilS6VEWubROAkd9f5XY0FDYQQw9JG8NGl2rT8vaO10Vp+58NHnD8u umYAHWvoag4r+XN22mzuYaun5ln5q40T6+aYnM= Received: by mailuser.nyi.internal (Postfix, from userid 99) id 3D71E168C1; Tue, 23 Aug 2016 12:13:35 -0400 (EDT) Message-Id: <1471968815.2343352.703756417.6A1C9C13@webmail.messagingengine.com> X-Sasl-Enc: B70dkaxcTVhX3Iddsutc/2HylBARE4MK5S0C0V/eMeFW 1471968815 From: CN To: pgsql-sql@postgresql.org MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain X-Mailer: MessagingEngine.com Webmail Interface - ajax-668fb998 Subject: Display group title only at the first record within each group Date: Wed, 24 Aug 2016 00:13:35 +0800 X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Hi! Such layout is commonly seen on real world reports where duplicated group titles are discarded except for the first one. CREATE TABLE x(name TEXT,dt DATE,amount INTEGER); COPY x FROM stdin; john 2016-8-20 80 mary 2016-8-17 20 john 2016-7-8 30 john 2016-8-19 40 mary 2016-8-17 30 john 2016-7-8 50 \. My desired result follows: john 2016-07-08 50 30 2016-08-19 40 2016-08-20 80 mary 2016-08-17 20 30 Note that "dt" is sorted as if clause ORDER BY name,dt was applied to SELECT. With this SELECT: SELECT name ,ROW_NUMBER() OVER (PARTITION BY name) AS rn_name ,dt ,ROW_NUMBER() OVER (PARTITION BY name,dt) AS rn_dt ,amount FROM x; I get this result: john 2 2016-07-08 1 30 john 4 2016-07-08 2 50 john 3 2016-08-19 1 40 john 1 2016-08-20 1 80 mary 1 2016-08-17 1 20 mary 2 2016-08-17 2 30 Above result shows that records are not sorted by rn_name and rn_dt. Were the above records correctly sorted "BY rn_name,rn_dt", the following SELECT probably would fulfill my ultimate goal: SELECT CASE WHEN rn_name=1 THEN name ELSE NULL END ,CASE WHEN rn_dt=1 THEN dt ELSE NULL END ,amount FROM ( SELECT name ,ROW_NUMBER() OVER (PARTITION BY name) AS rn_name ,dt ,ROW_NUMBER() OVER (PARTITION BY name,dt) AS rn_dt ,amount FROM x ) t Would someone please give me a hand? Best Regards, CN -- http://www.fastmail.com - IMAP accessible web-mail -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql