?
Solved

SQL Query

Posted on 2011-03-09
5
Medium Priority
?
309 Views
Last Modified: 2012-05-11
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!
0
Comment
Question by:Purdue_Pete
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 15

Accepted Solution

by:
derekkromm earned 800 total points
ID: 35084068
try these:
-- 0 only
select
	t1.person_id, count(*) cnt
from
	tbl t1
	left outer join tbl t2
		on	t1.person_id = t2.person_id
			and t2.type = 1
where
	t1.type = 0
	and t2.type is null
group by
	t1.person_id

-- 1 only
select
	t1.person_id, count(*) cnt
from
	tbl t1
	left outer join tbl t2
		on	t1.person_id = t2.person_id
			and t2.type = 0
where
	t1.type = 1
	and t2.type is null
group by
	t1.person_id


-- 0 and 1
select
	t1.person_id, count(*) cnt
from
	tbl t1
	inner join tbl t2
		on	t1.person_id = t2.person_id
			and t1.type = 0
			and t2.type = 1
group by
	t1.person_id

Open in new window

0
 
LVL 3

Assisted Solution

by:kraiven
kraiven earned 400 total points
ID: 35084120
Maybe I'm misunderstanding something but from your sample all you appear to need is:

select count(person_id) from TABLE where type = 0

Open in new window


Why the grouping in the WHERE clause?
0
 
LVL 15

Assisted Solution

by:derekkromm
derekkromm earned 800 total points
ID: 35084132
he wants only 0 and only 1

doing count where type=0 would return anyone that is both 0-only and 0-and-1
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 400 total points
ID: 35084188
to find the persons that are only 0:
select name from yourtable group by name having min(type) = 0 and max(type) = 0 

Open in new window


to find the persons that are only 1:
select name from yourtable group by name having min(type) = 1 and max(type) = 1 

Open in new window


to find the persons that are both 0 and 1:
select name from yourtable group by name having min(type) = 0 and max(type) = 1 

Open in new window


from there, you can subselect to count those ...
0
 
LVL 3

Assisted Solution

by:ccwill88
ccwill88 earned 400 total points
ID: 35084253
use exists to query:
a) are just type 0
select count(distinct person_id) from TABLE a where type = 0 AND person_id NOT EXISTS  (select * from TABLE b where type = 1  where a.person_id=b.person_id)  

Open in new window

b) are just type 1
select count(distinct person_id) from TABLE a where type = 1 AND person_id NOT EXISTS  (select * from TABLE b where type = 0  where a.person_id=b.person_id)  

Open in new window

c) are both type 0 and type 1
select count(distinct person_id) from TABLE a,TABLE b where  a.person_id=b.person_id
and a.type=0 and b.type=1  

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question