Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 2434F632B43 for ; Mon, 1 Feb 2010 06:11:10 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 30700-06 for ; Mon, 1 Feb 2010 10:10:54 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from forward15.mail.yandex.net (forward15.mail.yandex.net [95.108.130.119]) by mail.postgresql.org (Postfix) with ESMTP id 55EF0632A73 for ; Mon, 1 Feb 2010 06:10:58 -0400 (AST) Received: from webmail107.yandex.ru (webmail107.yandex.ru [95.108.131.132]) by forward15.mail.yandex.net (Yandex) with ESMTP id 3B93AC054A; Mon, 1 Feb 2010 13:10:56 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1265019056; bh=cMk7uUw5ENEdHcx8BaYpsl4gCYXxQc0vQaIExzqgyOg=; h=From:To:In-Reply-To:References:Subject:MIME-Version:Message-Id: Date:Content-Transfer-Encoding:Content-Type; b=hNp2C31L2dDgXkw4ZzaCEjf3gtyiyitULRIN75w7zF59BFQjMYTgTJmrAWwSipcuI rJ4lXuyKuo9fpaQXHj6pFJ16h6cyeBJeE6WvAKuFtzCgHTlVZUoslCDOIrmUw8lrac jj7nMDGTn4xNsvoLZyPVS47v3jSPdoEhBhPJ7MKE= Received: from localhost (localhost.localdomain [127.0.0.1]) by webmail107.yandex.ru (Yandex) with ESMTP id E027E1D44002; Mon, 1 Feb 2010 13:10:52 +0300 (MSK) X-Yandex-Spam: 1 X-Yandex-Front: webmail107 X-Yandex-TimeMark: 1265019052 Received: from user-20.81.118.217.in-addr.arpa (user-20.81.118.217.in-addr.arpa [217.118.81.20]) by mail.yandex.ru with HTTP; Mon, 01 Feb 2010 13:10:51 +0300 From: msi77 To: Neil Stlyz ,pgsql-sql@postgresql.org In-Reply-To: <995516.81823.qm@web113717.mail.gq1.yahoo.com> References: <995516.81823.qm@web113717.mail.gq1.yahoo.com> Subject: Re: combine SQL SELECT statements into one MIME-Version: 1.0 Message-Id: <37481265019051@webmail107.yandex.ru> Date: Mon, 01 Feb 2010 13:10:51 +0300 X-Mailer: Yamail [ http://yandex.ru ] 5.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=koi8-r X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=-10 required=5 tests=BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 201002/7 X-Sequence-Number: 33606 Hi, SELECT (SELECT COUNT(distinct model) FROM inventory WHERE modified >= '2010-02-01') as count1, (SELECT COUNT(distinct model) FROM inventory WHERE modified >= '2010-01-20') as count2, (SELECT COUNT(distinct model) FROM inventory WHERE modified >= '2010-01-01') as count3 Serge > Good Evening, Good Morning Wherever you are whenever you may be reading this. > I am new to this email group and have some good experience with SQL and PostgreSQL database. > I am currently working on a PHP / PostgreSQL project and I came upon something I could not figure out in SQL. I was wondering if anyone here could take a look and perhaps offer some guidance or assistance in helping me write this SQL query. > Please Consider the following information: > ------------------------------------------- > I have a postgresql table called 'inventory' that includes two fields: 'model' which is a character varying field and 'modified' which is a timestamp field. > So the table inventory looks something like this: > model modified > ------------- ---------- > I778288176 2010-02-01 08:27:00 > I778288176 2010-01-31 11:23:00 > I778288176 2010-01-29 10:46:00 > JKLM112345 2010-02-01 08:25:00 > JKLM112345 2010-01-31 09:52:00 > JKLM112345 2010-01-28 09:44:00 > X22TUNM765 2010-01-17 10:13:00 > V8893456T6 2010-01-01 09:17:00 > Now with the table, fields and data in mind look at the following three queries: > SELECT COUNT(distinct model) FROM inventory WHERE modified >= '2010-02-01'; > SELECT COUNT(distinct model) FROM inventory WHERE modified >= '2010-01-20'; > SELECT COUNT(distinct model) FROM inventory WHERE modified >= '2010-01-01'; > All three of the above queries work and provide results. However, I want to combine the three into one SQL Statement that hits the database one time. How can I do this in one SQL Statement? Is it possible with sub select? > Here is what result I am looking for from one SELECT statement using the data example from above: > count1 | count2 | count3 > ------------------------------- > 2 2 4 > Can this be done with ONE SQL STATEMENT? touching the database only ONE time? > Please let me know. > Thanx> :) > NEiL > Здесь спама нет http://mail.yandex.ru/nospam/sign