Solved

SQL Query

Posted on 2011-03-09
5
304 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 200 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 100 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 200 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 100 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 100 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

738 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