slightwv,
If you do not want the 'Either' category ... add
WHERE husband_state = 'AL' or wife_state = 'AL'
lwadwell
Main Topics
Browse All TopicsI've been staring at this to long and give up. I might be able to change/create the tables if necessary but I'd rather not.
Using Oracle 10.2.
Given a table of marriage_states (simplified of course):
create table tab1 (husband_state char(2), wife_state char(2));
insert into tab1 values('AL','WA');
insert into tab1 values('AL','NY');
insert into tab1 values('ND','AL');
insert into tab1 values('AL','AL');
insert into tab1 values('AL','AL');
insert into tab1 values('AL','AL');
commit;
What I would like from a single query (it's for a report) is:
- Husbands from AL with wives not from AL
- Wives from AL with husbands not from AL
- Couples where both are from AL
results
----------------------
Husbands 2
Wives 1
Couples 3
Best I can think of now involves 3 full table scans with 2 unions.
select 'Husbands', count(*) from tab1 where husband_state='AL and wife_state != 'AL
union
select 'Wives', count(*) from tab1 where husband_state!='AL and wife_state = 'AL
union
select 'Couples', count(*) from tab1 where husband_state='AL and wife_state = 'AL
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Not a lot of rows by today's standards, 1.5mil and counting but a lot of inserts, no updates and very rare deletes.
I was way ahead of you on indexes if not for the high number of inserts I'm a perfect fit for bitmap indexes. Regular B-Tree indexes would be skewed to fast and I'd need to many of them. Remember, I simplified the table a bit. I have a few of these types of pairs I need to report against.
I'm trying to squeeze the last few seconds out of a 30 second web report and knew there had to be a better way than a 3 way full table scan. I was just way to deep into the problem and couldn't see the simple path.
If the 'case' select works I'm going to kick myself. I use them throughout the application in other places. :(
Business Accounts
Answer for Membership
by: lwadwellPosted on 2009-06-11 at 16:44:32ID: 24607987
Hi slightwv,
try something like ...
SELECT
CASE WHEN husband_state = 'AL' and wife_state = 'AL' THEN 'Couples'
WHEN husband_state = 'AL' THEN 'Husbands'
WHEN wife_state = 'AL' THEN 'Wifes'
ELSE 'Neither'
END as category, count(*)
FROM tab1
GROUP BY
CASE WHEN husband_state = 'AL' and wife_state = 'AL' THEN 'Couples'
WHEN husband_state = 'AL' THEN 'Husbands'
WHEN wife_state = 'AL' THEN 'Wifes'
ELSE 'Neither'
END
lwadwell