Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WQIii-0006zQ-4e for pgsql-sql@arkaria.postgresql.org; Wed, 19 Mar 2014 15:47:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WQIih-0003Q3-GE for pgsql-sql@arkaria.postgresql.org; Wed, 19 Mar 2014 15:47:55 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WQIig-0003Pw-Eg for pgsql-sql@postgresql.org; Wed, 19 Mar 2014 15:47:54 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WQIid-0005pl-PP for pgsql-sql@postgresql.org; Wed, 19 Mar 2014 15:47:53 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1WQIic-0003I5-R8 for pgsql-sql@postgresql.org; Wed, 19 Mar 2014 08:47:50 -0700 Date: Wed, 19 Mar 2014 08:47:50 -0700 (PDT) From: David Johnston To: pgsql-sql@postgresql.org Message-ID: <1395244070835-5796803.post@n5.nabble.com> In-Reply-To: References: Subject: Re: Table results format - should I use crosstab? If so, how? MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 4.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 Jennifer Mackown wrote > Hi, > I have a problem with getting a table to display in the way I want it to. > It's one of those things that looks so simple I should be able to do it in > 5 minutes, but I've been working on it all afternoon and I'm getting > nowhere!! > What I have is the following: > Date Firstday Lastday2014/03/12 1 > 12014/03/18 1 02014/03/19 0 > 12014/03/21 1 1 > > And what I need to see is this: > Firstday Lastday2014/03/12 2013/03/122014/03/18 > 2013/03/192014/03/21 2013/03/21 WITH data (dt, isfirst, islast) AS ( --setup data VALUES ('2014-03-12'::date, true, true), ('2013-03-18', true, false), ('2013-03-19', false, true), ('2013-03-21', true, true) ) , explode AS ( --need to convert columns to rows; use UNION ALL to do this SELECT dt, 1 AS pos FROM data WHERE isfirst --all start rows UNION ALL SELECT dt, 2 AS pos FROM data WHERE islast --all end rows ) , ordered AS ( --need to arrange the rows so start comes before end SELECT * FROM explode ORDER BY dt ASC, pos ) SELECT firstday, lastday FROM ( --then for each end row in the pair get the immediately prior row as its start SELECT pos, dt AS lastday, lag(dt) OVER () AS firstday FROM ordered ) calc WHERE pos = 2 ORDER BY firstday DESC --and only display the end rows ; This directly solves the problem, however: 1) Start & End dates must be defined in pairs (none missing and no extra dates in between) 2) There can be no overlapping ranges Ideally you would have some kind of identifier attached to every start date and a corresponding identifier on the matching end date. Partitioning on that identifier and taking the first and last date found would be the most stable method. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/Table-results-format-should-I-use-crosstab-If-so-how-tp5796797p5796803.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql