If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > SQL Question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-22-04, 17:04
tamamakama tamamakama is offline
Registered User
 
Join Date: Aug 2004
Posts: 2
SQL Question

I have a sql problem. Consider this table:

+---------+-------------+-------+-------+
| issueid | fieldbankid | cond1 | cond2 |
+---------+-------------+-------+-------+
| 1 | 1 | 1 | 0 |
| 1 | 2 | 0 | 1 |
| 2 | 1 | 0 | 0 |
| 2 | 2 | 0 | 0 |
| 3 | 1 | 1 | 0 |
| 3 | 2 | 0 | 0 |
| 4 | 1 | 0 | 0 |
| 4 | 2 | 0 | 0 |
+---------+-------------+-------+-------+

I want to pull out the issueids where cond1 and cond2
are both > 0.

By inspecting the data above, the answer is of course
issueid 1.

But I can't figure out what the sql statement to use
would be...

Here are the statements to build the test case:

create table test (
issueid int(255),
fieldbankid int(255),
cond1 int (255),
cond2 int (255)
);

insert into test values (1, 1, 1, 0);
insert into test values (1, 2, 0, 1);
insert into test values (2, 1, 0, 0);
insert into test values (2, 2, 0, 0);
insert into test values (3, 1, 1, 0);
insert into test values (3, 2, 0, 0);
insert into test values (4, 1, 0, 0);
insert into test values (4, 2, 0, 0);

I would like to create most efficient query possible, avoiding correlated subqueries if possible. Any help would be greatly appreciated!
Reply With Quote
  #2 (permalink)  
Old 08-22-04, 20:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
select issueid 
  from test
 where cond1 = 1
    or cond2 = 1
group
    by issueid
having count(*) = 2
this is the simplest, and assumes there can be only two rows for each id as in your test sample

if there can be more, you'll need slightly different sql
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On