×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Some illogical query results

Some illogical query results

Some illogical query results

(OP)
I've gotten the following:

db=# select count(*) from table1 where field1 in (select field2 from table2);
 count
-------
   175
(1 row)

db=# select count(*) from table1 where field1 not in (select field2 from table2);
 count
-------
     0
(1 row)

db=# select count(*) from table1;
 count
-------
   955
(1 row)


OK, isn't this mathematically impossible?  Any idea what's going on here?

RE: Some illogical query results

Yes, most likely you have some NULLs involved here. Aggregate functions such as COUNT(*) cannot perform certain kinds of logic with a NULL, because NULL doesn't mean "no value"; in relational query terms it means "we don't know if there is a value", thus it can't logically tell whether field1 does or does not match field2. This is why many serious DB practitioners will caution you to avoid creating columns that allow NULLs unless you have a specific reason to do so.

-------------------------------------------

Big Brother: "War is Peace" -- Big Business: "Trust is Suspicion"
(http://www.cl.cam.ac.uk/~rja14/tcpa-faq.html)

RE: Some illogical query results

(OP)
Now I've got a new problem.  I've set table2 to mirror table1 in structure, and now I've dumped new data into table2.  I'm trying to insert all the data from table2 into table1, but I keep getting this error:

ERROR:  Cannot insert a duplicate key into unique index indx_unique_table1_field

indx_unique_table1_field is a unique index I set up on the column "field" in table1.  However, when I search for these duplicate values, I get the following:

select field from table2 where field in (select field from table1);
 field
-------
(0 rows)


There are no null values in either of the columns in question.

RE: Some illogical query results

How about showing us the structure of both tables? (pg_dump -s)

-------------------------------------------

Big Brother: "War is Peace" -- Big Business: "Trust is Suspicion"
(http://www.cl.cam.ac.uk/~rja14/tcpa-faq.html)

RE: Some illogical query results

(OP)
Here's the two tables:
CREATE TABLE "tbclients" (
    "id" integer DEFAULT nextval('"seq_tbclients_id"'::text) NOT NULL,
    "ssn" character varying(11),
    "firstname" character varying(20),
    "lastname" character varying(30),
    "street" character varying(50),
    "city" character varying(25),
    "state" character varying(2) DEFAULT 'IN',
    "zipcode" integer,
    "phone" character varying(14),
    "dob" date,
    "sex" character varying(6),
    "township" character varying(20),
    "veteran" boolean,
    "ethnicity" character varying(20),
    "maritalstatus" character varying(20),
    "residentdate" date,
    "church" character varying(50),
    "insurance" character varying(50),
    "resident" character varying(15),
    "homeless" boolean,
    Constraint "tbclients_pkey" Primary Key ("id")
);


CREATE TABLE "ccsnclients" (
    "id" integer DEFAULT nextval('"seq_ccsnclients_id"'::text) NOT NULL,
    "ssn" character varying(11),
    "firstname" character varying(20),
    "lastname" character varying(30),
    "street" character varying(50),
    "city" character varying(25),
    "state" character varying(2) DEFAULT 'IN',
    "zipcode" integer,
    "phone" character varying(14),
    "dob" date,
    "sex" character varying(6),
    "township" character varying(20),
    "veteran" boolean,
    "ethnicity" character varying(20),
    "maritalstatus" character varying(20),
    "residentdate" date,
    "church" character varying(50),
    "insurance" character varying(50),
    "resident" character varying(15),
    "homeless" boolean,
    Constraint "ccsnclients_pkey" Primary Key ("id")
);


I'm trying to insert data from ccsnclients into tbclients.  I use the SQL command

insert into tbclients (select * from ccsnclients);

and get the error message

ERROR:  Cannot insert a duplicate key into unique index indx_unique_tbclients_ssn

RE: Some illogical query results

(OP)
Never mind, I've discovered the problem.  The "ccsnclients" table had some duplicate values in the ssn column.  I thought I had taken care of that, but apparently when we made the last update those duplicate values reappeared.  

"Don't try this at home.  This should only be done by trained professional idiots." :-b

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close