Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id B03FC9F9A72 for ; Sat, 18 Aug 2007 02:35:16 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 99455-09 for ; Sat, 18 Aug 2007 02:35:14 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from wa-out-1112.google.com (wa-out-1112.google.com [209.85.146.181]) by postgresql.org (Postfix) with ESMTP id 050729F9A44 for ; Sat, 18 Aug 2007 02:35:13 -0300 (ADT) Received: by wa-out-1112.google.com with SMTP id v27so85930wah for ; Fri, 17 Aug 2007 22:35:12 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition:x-google-sender-auth; b=NpGZgvAbC7W2pCiQGoR7f79CSlF9Ljj7q5EuSkD25RsilnSD9BPxxGJ1G67IRv6sGL37xiz92D6UNnH629u+OS5sUpsUFufb/V8ZEbWU5aNkHvjVLNXKvAchfsxzNmV5wUvEaWMat2tSk4CVPHfey05XeHQT2PrM+WDbfXgZ434= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:sender:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition:x-google-sender-auth; b=NUfC7lxWmG+VNEYK/7LQnFi9Xi8ZVNHTX6xk5tmGweaaETbphwru1FI7Qxjmk64KgHmR2KhaGqasnL+tds7ewFGnCaAYZavkw7LNj4RANT2gsl2NmsZpD5mvn11bUl9llpa67EKdllXaN/V3Vv5LveylUNW5bp9W0ZmRK/M46Us= Received: by 10.115.60.1 with SMTP id n1mr4018840wak.1187415312663; Fri, 17 Aug 2007 22:35:12 -0700 (PDT) Received: by 10.114.80.13 with HTTP; Fri, 17 Aug 2007 22:35:12 -0700 (PDT) Message-ID: Date: Sat, 18 Aug 2007 15:35:12 +1000 From: novice To: pgsql-sql@postgresql.org Subject: Join query help MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Google-Sender-Auth: 83e017670bfb491a X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200708/152 X-Sequence-Number: 29090 Hi, We have the following three tables. safety=> SELECT record_id, record_date FROM record; record_id | record_date -----------+------------------------ 1 | 2007-07-23 11:30:37+10 2 | 2007-07-27 11:30:14+10 3 | 2007-07-17 13:15:03+10 (3 rows) safety=> SELECT observation_id, record_id, score_id FROM observation; observation_id | record_id | score_id ----------------+-----------+---------- 3240 | 1 | 1 3239 | 1 | 1 3238 | 1 | 2 3237 | 1 | 1 2872 | 2 | 1 2869 | 2 | 2 2870 | 2 | 1 2871 | 2 | 1 3218 | 3 | 2 3217 | 3 | 1 (10 rows) safety=> SELECT * FROM SCORE; score_id | score_description ----------+------------------- 0 | NA 1 | SAFE 2 | AT RISK (3 rows) What query do I write to generate the following? week_no | count(record_id | count(observation_id) | sum(score_id) where = '1' 2007, 30 | 2 | 8 | 6 2007, 29 | 1 | 2 | 1