X-Original-To: pgsql-sql-postgresql.org@postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id A645B9FB1C0 for ; Thu, 6 Jul 2006 04:00:27 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 32764-04 for ; Thu, 6 Jul 2006 04:00:19 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey- Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.168]) by postgresql.org (Postfix) with ESMTP id 1AADB9FA603 for ; Thu, 6 Jul 2006 04:00:18 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id a2so2559976ugf for ; Thu, 06 Jul 2006 00:00:17 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=abzQ5Kvm2ys7TpaZZEdNFBmy//7orLLbfs2ACnKaOMO0XbzlkHhbvGjoTNxxRdcL/wQYtspWb+SJJRqYxoC6+Bqjolkw6okD/Og8EcsWi50Mz3kFRs4208vIMsuoWMdCVohF85/9wIj2XJ7QPjRSnSRZir/Wd7g0ayi8nfV4IxY= Received: by 10.67.26.7 with SMTP id d7mr326741ugj; Thu, 06 Jul 2006 00:00:17 -0700 (PDT) Received: by 10.67.24.20 with HTTP; Thu, 6 Jul 2006 00:00:17 -0700 (PDT) Message-ID: Date: Thu, 6 Jul 2006 02:00:17 -0500 From: "Rodrigo De Leon" Reply-To: rdeleonp@gmail.com, rdeleonp@yahoo.com To: pgsql-sql@postgresql.org Subject: Re: week ending In-Reply-To: <44AC8F6A.4090704@NarrowPathInc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <44AC8F6A.4090704@NarrowPathInc.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200607/52 X-Sequence-Number: 25626 On 7/5/06, Keith Worthington 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 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