agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
Can I simplify this somehow?
4+ messages / 3 participants
[nested] [flat]

* Can I simplify this somehow?
@ 2013-09-27 15:22 Larry Rosenman <ler@lerctr.org>
  2013-09-27 19:24 ` Re: Can I simplify this somehow? Adam Jelinek <ajelinek@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Larry Rosenman @ 2013-09-27 15:22 UTC (permalink / raw)
  To: pgsql-sql

I tried(!) to write this as a with (CTE), but failed.

Can one of the CTE experts (or better SQL writer) help me here?

-- generate a table of timestamps to match against
select
generate_series(date_trunc('day',now()-'45 days'::interval),now()+'1 
hour'::inte
rval,'1 hour')
    AS thetime  into temp table timestamps;

-- get a count of logged in users for a particular time
SELECT thetime,case extract(dow  from thetime)
                when 0 then 'Sunday'
                when 1 then 'Monday'
                when 2 then 'Tuesday'
                when 3 then 'Wednesday'
                when 4 then 'Thursday'
                when 5 then 'Friday'
                when 6 then 'Saturday' end AS "Day", count(*) AS 
"#LoggedIn"
FROM  timestamps,user_session
WHERE thetime BETWEEN login_time AND COALESCE(logout_time, now())
GROUP BY thetime
ORDER BY thetime;

Thanks for any help at all.


-- 
Larry Rosenman                     http://www.lerctr.org/~ler
Phone: +1 214-642-9640 (c)     E-Mail: ler@lerctr.org
US Mail: 108 Turvey Cove, Hutto, TX 78634-5688


-- 
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] 4+ messages in thread

* Re: Can I simplify this somehow?
  2013-09-27 15:22 Can I simplify this somehow? Larry Rosenman <ler@lerctr.org>
@ 2013-09-27 19:24 ` Adam Jelinek <ajelinek@gmail.com>
  2013-09-27 19:32   ` Re: Can I simplify this somehow? Larry Rosenman <ler@lerctr.org>
  0 siblings, 1 reply; 4+ messages in thread

From: Adam Jelinek @ 2013-09-27 19:24 UTC (permalink / raw)
  To: Larry Rosenman <ler@lerctr.org>; +Cc: pgsql-sql

Are you looking for something like this for the result for the last 45 days
or something else?

TIME  MON   TUE  WED ...
09:00     1        3       4
10:00     5        0       8


On Fri, Sep 27, 2013 at 10:22 AM, Larry Rosenman <ler@lerctr.org> wrote:

> I tried(!) to write this as a with (CTE), but failed.
>
> Can one of the CTE experts (or better SQL writer) help me here?
>
> -- generate a table of timestamps to match against
> select
> generate_series(date_trunc('**day',now()-'45 days'::interval),now()+'1
> hour'::inte
> rval,'1 hour')
>    AS thetime  into temp table timestamps;
>
> -- get a count of logged in users for a particular time
> SELECT thetime,case extract(dow  from thetime)
>                when 0 then 'Sunday'
>                when 1 then 'Monday'
>                when 2 then 'Tuesday'
>                when 3 then 'Wednesday'
>                when 4 then 'Thursday'
>                when 5 then 'Friday'
>                when 6 then 'Saturday' end AS "Day", count(*) AS "#LoggedIn"
> FROM  timestamps,user_session
> WHERE thetime BETWEEN login_time AND COALESCE(logout_time, now())
> GROUP BY thetime
> ORDER BY thetime;
>
> Thanks for any help at all.
>
>
> --
> Larry Rosenman                     http://www.lerctr.org/~ler
> Phone: +1 214-642-9640 (c)     E-Mail: ler@lerctr.org
> US Mail: 108 Turvey Cove, Hutto, TX 78634-5688
>
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/**mailpref/pgsql-sql<http://www.postgresql.org/mailpref/pgsql-sql;
>

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

* Re: Can I simplify this somehow?
  2013-09-27 15:22 Can I simplify this somehow? Larry Rosenman <ler@lerctr.org>
  2013-09-27 19:24 ` Re: Can I simplify this somehow? Adam Jelinek <ajelinek@gmail.com>
@ 2013-09-27 19:32   ` Larry Rosenman <ler@lerctr.org>
  2013-09-27 23:12     ` Re: Can I simplify this somehow? Sergey Konoplev <gray.ru@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Larry Rosenman @ 2013-09-27 19:32 UTC (permalink / raw)
  To: Adam Jelinek <ajelinek@gmail.com>; +Cc: pgsql-sql

On 2013-09-27 14:24, Adam Jelinek wrote:
> Are you looking for something like this for the result for the last 45
> days or something else?
> 
> TIME  MON   TUE  WED ...
> 09:00     1        3       4
> 10:00     5        0       8
> 
That would be cool, but just a list is good too.....



-- 
Larry Rosenman                     http://www.lerctr.org/~ler
Phone: +1 214-642-9640 (c)     E-Mail: ler@lerctr.org
US Mail: 108 Turvey Cove, Hutto, TX 78634-5688


-- 
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] 4+ messages in thread

* Re: Can I simplify this somehow?
  2013-09-27 15:22 Can I simplify this somehow? Larry Rosenman <ler@lerctr.org>
  2013-09-27 19:24 ` Re: Can I simplify this somehow? Adam Jelinek <ajelinek@gmail.com>
  2013-09-27 19:32   ` Re: Can I simplify this somehow? Larry Rosenman <ler@lerctr.org>
@ 2013-09-27 23:12     ` Sergey Konoplev <gray.ru@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Sergey Konoplev @ 2013-09-27 23:12 UTC (permalink / raw)
  To: Larry Rosenman <ler@lerctr.org>; +Cc: Adam Jelinek <ajelinek@gmail.com>; pgsql-sql

On Fri, Sep 27, 2013 at 12:32 PM, Larry Rosenman <ler@lerctr.org> wrote:
> On 2013-09-27 14:24, Adam Jelinek wrote:
>> TIME  MON   TUE  WED ...
>> 09:00     1        3       4
>> 10:00     5        0       8
>>
> That would be cool, but just a list is good too.....

You need to take a look at this module:

http://www.postgresql.org/docs/9.3/static/tablefunc.html

Particularly look at the crostab(text, text) function.

-- 
Kind regards,
Sergey Konoplev
PostgreSQL Consultant and DBA

http://www.linkedin.com/in/grayhemp
+1 (415) 867-9984, +7 (901) 903-0499, +7 (988) 888-1979
gray.ru@gmail.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] 4+ messages in thread


end of thread, other threads:[~2013-09-27 23:12 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-09-27 15:22 Can I simplify this somehow? Larry Rosenman <ler@lerctr.org>
2013-09-27 19:24 ` Adam Jelinek <ajelinek@gmail.com>
2013-09-27 19:32   ` Larry Rosenman <ler@lerctr.org>
2013-09-27 23:12     ` Sergey Konoplev <gray.ru@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