pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
week ending
3+ messages / 3 participants
[nested] [flat]

* week ending
@ 2006-07-06 04:19  Keith Worthington <KeithW@NarrowPathInc.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Keith Worthington @ 2006-07-06 04:19 UTC (permalink / raw)
  To: pgsql-sql

Hi All,

I just finished writing a query that groups data based on the week number.

SELECT EXTRACT(week FROM col_a) AS week_number,
        sum(col_b) AS col_b_total
   FROM foo
  WHERE foobar
  GROUP BY EXTRACT(week FROM col_a)
  ORDER BY EXTRACT(week FROM col_a);

I would like to generate the starting date or ending date based on this 
number.  IOW instead of telling the user "week number" which they won't 
understand I would like to provide either Friday's date for "week 
ending" or Monday's date for "week beginning".

SELECT <something> AS week_ending,
        sum(col_b) AS col_b_total
   FROM foo
  WHERE foobar
  GROUP BY EXTRACT(week FROM col_a)
  ORDER BY EXTRACT(week FROM col_a);

-- 

Kind Regards,
Keith



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

* Re: week ending
@ 2006-07-06 07:00  Rodrigo De Leon <rdeleonp@gmail.com>
  parent: Keith Worthington <KeithW@NarrowPathInc.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Rodrigo De Leon @ 2006-07-06 07:00 UTC (permalink / raw)
  To: pgsql-sql

On 7/5/06, Keith Worthington <KeithW@narrowpathinc.com> wrote:
> Hi All,
>
> I just finished writing a query that groups data based on the week number.
>
> SELECT EXTRACT(week FROM col_a) AS week_number,
>         sum(col_b) AS col_b_total
>    FROM foo
>   WHERE foobar
>   GROUP BY EXTRACT(week FROM col_a)
>   ORDER BY EXTRACT(week FROM col_a);
>
> I would like to generate the starting date or ending date based on this
> number.  IOW instead of telling the user "week number" which they won't
> understand I would like to provide either Friday's date for "week
> ending" or Monday's date for "week beginning".
>
> SELECT <something> AS week_ending,
>         sum(col_b) AS col_b_total
>    FROM foo
>   WHERE foobar
>   GROUP BY EXTRACT(week FROM col_a)
>   ORDER BY EXTRACT(week FROM col_a);
>
> --
>
> Kind Regards,
> Keith

select
max(case when (to_char(col_a,'d') between 2 and 6) then col_a end) as
week_ending,
sum(col_b) as col_b_total from foo
where foobar
group by extract(year from col_a), extract(week from col_a)
order by extract(year from col_a), extract(week from col_a);

Regards,

Rodrigo



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

* Re: week ending
@ 2006-07-06 15:49  Aaron Bono <postgresql@aranya.com>
  parent: Keith Worthington <KeithW@NarrowPathInc.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Aaron Bono @ 2006-07-06 15:49 UTC (permalink / raw)
  To: KeithW@narrowpathinc.com; +Cc: pgsql-sql

On 7/5/06, Keith Worthington <KeithW@narrowpathinc.com> wrote:
>
> Hi All,
>
> I just finished writing a query that groups data based on the week number.
>
> SELECT EXTRACT(week FROM col_a) AS week_number,
>         sum(col_b) AS col_b_total
>    FROM foo
>   WHERE foobar
>   GROUP BY EXTRACT(week FROM col_a)
>   ORDER BY EXTRACT(week FROM col_a);
>
> I would like to generate the starting date or ending date based on this
> number.  IOW instead of telling the user "week number" which they won't
> understand I would like to provide either Friday's date for "week
> ending" or Monday's date for "week beginning".
>
> SELECT <something> AS week_ending,
>         sum(col_b) AS col_b_total
>    FROM foo
>   WHERE foobar
>   GROUP BY EXTRACT(week FROM col_a)
>   ORDER BY EXTRACT(week FROM col_a);


Try this.  It puts Saturday as the Friday before it and Sunday as the Firday
after so if you want Saturday or Sunday to be on different weeks you will
need to do a little tweaking but this should get you going.

SELECT
   date_trunc('day', col_a + (interval '1 day' * (5 - extract(dow from
col_a)))) AS week_ending,
   sum(col_b) AS col_b_total
FROM foo
GROUP BY
   date_trunc('day', col_a + (interval '1 day' * (5 - extract(dow from
col_a))))

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


end of thread, other threads:[~2006-07-06 15:49 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-07-06 04:19 week ending Keith Worthington <KeithW@NarrowPathInc.com>
2006-07-06 07:00 ` Rodrigo De Leon <rdeleonp@gmail.com>
2006-07-06 15:49 ` Aaron Bono <postgresql@aranya.com>

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