pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Thorsen <mthorsen1980@gmail.com>
To: pgsql-general@postgresql.org
Subject: SQL query
Date: Fri, 21 Nov 2008 22:33:33 -0600
Message-ID: <4c8d33a90811212033x5e88177eo7a026b10336c975b@mail.gmail.com> (raw)

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

view thread (45+ messages)  latest in thread

Message-ID: <4c8d33a90811212033x5e88177eo7a026b10336c975b@mail.gmail.com>
Permalink:  ../4c8d33a90811212033x5e88177eo7a026b10336c975b@mail.gmail.com/
Also on:    postgresql.org/message-id/4c8d33a90811212033x5e88177eo7a026b10336c975b@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: mthorsen1980@gmail.com
  Subject: Re: SQL query
  In-Reply-To: <4c8d33a90811212033x5e88177eo7a026b10336c975b@mail.gmail.com>
  References: <4c8d33a90811212033x5e88177eo7a026b10336c975b@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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