Link to home
Start Free TrialLog in
Avatar of Purdue_Pete
Purdue_Pete

asked on

SQL Query

Hi,

Following is the table Structure in DB:
id: auto increment integer
name: varchar(256)
person_id: an integer that references a person in a different table
type: two values - 0 or 1

0 means a person w/ type0
1 means a person w/ type1

Following values and (others not mentioned) are possible:
1 XXX 1 0
2 XXX 1 1
3 YYY 2 0
4 ZZZ 3 1
5 AAA 2 0

The following are what I like to find out - number of persons who
a) are just type 0
b) are just type 1
c) are both type 0 and type 1

Initially, I wrote a simple query to answer a)
select count(person_id) from TABLE where type = 0 AND person_id NOT IN (select person_id from TABLE where type = 1 group by person_id) group by person_id;

But, it is too slow to compute - almost ran for 10 hours and still not completed.
There is a index for each column - id, person_id, type

Please provide new SQL queries that is faster and does the above job.

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of derekkromm
derekkromm
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial