Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 87ED164FC39 for ; Sat, 22 Nov 2008 00:33:38 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 88242-08 for ; Sat, 22 Nov 2008 00:33:35 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from fg-out-1718.google.com (fg-out-1718.google.com [72.14.220.153]) by mail.postgresql.org (Postfix) with ESMTP id 7E45A64FC36 for ; Sat, 22 Nov 2008 00:33:35 -0400 (AST) Received: by fg-out-1718.google.com with SMTP id 16so977722fgg.23 for ; Fri, 21 Nov 2008 20:33:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:mime-version:content-type; bh=vf5E8g1IEfO5+G2LQzz51N/nhtvkJRkp2tT9CQHDAMU=; b=SDjRwHhMsSt4/D8TwGskmZCmWqoScFOnrHkvIujV/jA/13EUQSKKlwcEewwr1VliY+ cY6A1GzNEVLfxEF8MWvWfD0TgbXUszFCEq1fwSDTc78dze1rmb2ZY3+LIAoEt1jfj75R FC2b0RiBx5e6sue6bHZwgPHvXCYAxLxYLswJQ= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:mime-version:content-type; b=JB1o7R+pzn50+Sb71l6SzL1R4UlG49HwSA6qGAjprk2W4OZR3Gz0mTYl06TE5GPnNA IDQnGXE2tbhzU8v+QDpmgj1Rq54wbpEXAZzARfCuMRZ7WkxvnpivoKhqzW25uSpRmk6d LkL+VfgF7MLGBfqjb9j9IXMVKGual4PLg3Lp0= Received: by 10.181.61.9 with SMTP id o9mr348977bkk.86.1227328413844; Fri, 21 Nov 2008 20:33:33 -0800 (PST) Received: by 10.181.50.20 with HTTP; Fri, 21 Nov 2008 20:33:33 -0800 (PST) Message-ID: <4c8d33a90811212033x5e88177eo7a026b10336c975b@mail.gmail.com> Date: Fri, 21 Nov 2008 22:33:33 -0600 From: "Michael Thorsen" To: pgsql-general@postgresql.org Subject: SQL query MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_110708_25560967.1227328413821" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.001 tagged_above=0 required=5 tests=HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 200811/1139 X-Sequence-Number: 140857 ------=_Part_110708_25560967.1227328413821 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline I am running the Postgres(8.2.11) on Windows. I have 2 tables, one with users and one with locations. user_table --------------- user_id user_code price value 1 22222 45.23 -97.82 2 33333 42.67 -98.32 3 44444 35.56 -76.32 locations -------------- id code price value 1 22222 45.23 -97.82 2 33333 42.67 -98.32 3 44444 43.26 -98.65 I have a query that takes every user and looks into locations to see if the code, price and value match up. If they do then count it. select count(*) from user_table u, locations l where u.user_code = l.code and u.price = l.price and u.value = l.value; The answer to this should be 2, but when I run my query I get 4 (in fact more entries than user_table) which is incorrect. What am I doing incorrectly? I have been breaking my head over this for a while. Is there some other query to get the required results? Any help would be highly appreciated. I gave a simple example above, but the query runs over 2 tables with about a million entries in each. So I am unable to verify what is wrong, but I know the count is incorrect as I should not have more than what is in the user_table. Thanks, Michael ------=_Part_110708_25560967.1227328413821 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline I am running the Postgres(8.2.11) on Windows.

I have 2 tables, one with users and one with locations.

user_table
---------------
user_id      user_code      price           value
1               22222            45.23      -97.82
2               33333             42.67      -98.32
3               44444             35.56     -76.32

locations
--------------
id       code     price        value
1        22222   45.23     -97.82
2        33333   42.67      -98.32
3        44444   43.26     -98.65

I have a query that takes every user and looks into locations to see if the code, price and value match up. If they do then count it.

select count(*)
  from user_table u, locations l
 where u.user_code = l.code
    and u.price = l.price
    and u.value = l.value;

The answer to this should be 2, but when I run my query I get 4 (in fact more entries than user_table) which is incorrect. What am I doing incorrectly? I have been breaking my head over this for a while. Is there some other query to get the required results? Any help would be highly appreciated. I gave a simple example above, but the query runs over 2 tables with about a million entries in each. So I am unable to verify what is wrong, but I know the count is incorrect as I should not have more than what is in the user_table.

Thanks,
Michael

------=_Part_110708_25560967.1227328413821--