Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sql question

Status
Not open for further replies.

habesha

Programmer
Oct 30, 2006
68
0
0
US
Given this table

col1 col2
1 yes
1 no
2 yes
2 yes
3 no
3 no
4 yes
4 no

I want to query out that for each value in col1 if its corresponding value in col2 is yes, yes - it will be yes,
no, yes - it will be yes, no, no it will be no

the result set should look like

col1 col2
1 yes
2 yes
3 no
4 yes

thanks

 
Code:
select col1, max(col2) from @temp
group by col1

Also, you might want to stop naming your threads 'SQL Question' and be a bit more specific.

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
Here you go
Code:
create table blah(col1 int,    col2 varchar(666))
insert blah values(1,'yes')
insert blah values(1,'no')
insert blah values(2,'yes')
insert blah values(2,'yes')
insert blah values(3,'no')
insert blah values(3,'no')
insert blah values(4,'yes')
insert blah values(4,'no')

select col1, case max(col2) when 'no' then 'no' else 'yes' end col2
from blah
group by col1

Denis The SQL Menace
SQL blog:
 
code got cut ;-(

Code:
create table blah(col1 int,    col2 varchar(666))
insert blah values(1,'yes')
insert blah values(1,'no')
insert blah values(2,'yes')
insert blah values(2,'yes')
insert blah values(3,'no')
insert blah values(3,'no')
insert blah values(4,'yes')
insert blah values(4,'no')

select col1, case max(col2) when 'no' then 'no' else 'yes' end col2
from blah
group by col1

or simpler

select col1, max(col2) 
from blah
group by col1

Denis The SQL Menace
SQL blog:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top