Solved

SQL Query

Posted on 2011-03-09
5
299 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
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 142

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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now