agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
Table results format - should I use crosstab? If so, how?
3+ messages / 3 participants
[nested] [flat]

* Table results format - should I use crosstab? If so, how?
@ 2014-03-19 14:58  Jennifer Mackown <niftyshellsuit@outlook.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Jennifer Mackown @ 2014-03-19 14:58 UTC (permalink / raw)
  To: pgsql-sql

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


Can anyone help?
Thanks, 
Jennifer  		 	   		  =

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Table results format - should I use crosstab? If so, how?
@ 2014-03-19 15:47  David Johnston <polobo@yahoo.com>
  parent: Jennifer Mackown <niftyshellsuit@outlook.com>
  1 sibling, 0 replies; 3+ messages in thread

From: David Johnston @ 2014-03-19 15:47 UTC (permalink / raw)
  To: pgsql-sql

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-tp57967...
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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Table results format - should I use crosstab? If so, how?
@ 2014-03-19 17:56  hari.fuchs@gmail.com
  parent: Jennifer Mackown <niftyshellsuit@outlook.com>
  1 sibling, 0 replies; 3+ messages in thread

From: hari.fuchs@gmail.com @ 2014-03-19 17:56 UTC (permalink / raw)
  To: pgsql-sql

Jennifer Mackown <niftyshellsuit@outlook.com> writes:

> 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
>
>
> Can anyone help?

WITH tmp (id, firstday, lastday) AS (
  SELECT row_number() OVER (PARTITION BY t1.date ORDER BY t2.date),
         t1.date, t2.date
  FROM tbl t1
  JOIN tbl t2 ON t2.date >= t1.date
    AND t2.lastday = 1
  WHERE t1.firstday = 1
)
SELECT id, firstday, lastday
FROM tmp
WHERE id = 1
ORDER BY firstday, lastday



-- 
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2014-03-19 17:56 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-03-19 14:58 Table results format - should I use crosstab? If so, how? Jennifer Mackown <niftyshellsuit@outlook.com>
2014-03-19 15:47 ` David Johnston <polobo@yahoo.com>
2014-03-19 17:56 ` hari.fuchs@gmail.com

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