Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
alwayslearning
Starting Member
2 Posts |
Posted - 2011-04-15 : 01:49:47
|
| Hey everyone - had a question about this sql query I'm trying to write. I'm really having some difficulty writing the "where" clause. Any help would be greatly appreciated!Imagine that all of the following resides in the same table:[Table A]Column AGregSueSamGregSueTomFrankGregColumn BRedBluePinkBlueRedGreenPinkPinkColumn C1239432238902389432823082384020438SELECT Column A, Column B, Column CFROM [TABLE A]WHERE (i don't know this part....where the people in column A has "red" and "blue" record)The results should be this:Column AGregGregSueSueColumn BRedBlueBlueRedColumn C123238949432328Any help would be greatly appreciated! |
|
|
raghuveer125
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-04-15 : 02:25:41
|
| Try thisselect * from TableA where ColumnB='Red' or ColumnB='Blue'Raghu' S |
 |
|
|
Ranjit.ileni
Posting Yak Master
183 Posts |
Posted - 2011-04-15 : 06:35:07
|
| alsoselect * from TableAwhere ColumnB in('Red','Blue')--Ranjit |
 |
|
|
alwayslearning
Starting Member
2 Posts |
Posted - 2011-04-15 : 13:39:40
|
| Thanks for the response! Using the Where...in clause brings back people who had red or blue. I'd like to return the people who identified "red" AND "blue". |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-04-15 : 13:48:59
|
| DECLARE @TableA TABLE (ColA varchar(10),COLB varchar(10))INSERT INTO @TABLEA (ColA,COLB)SELECT 'Greg' ,'Red' UNION ALLSELECT 'Sue','Blue' UNION ALLSELECT 'Sam','Pink' UNION ALLSELECT 'Greg','Blue' UNION ALLSELECT 'Sue','Red' UNION ALLSELECT 'Tom','Green' UNION ALLSELECT 'Frank' ,'Pink'UNION ALLSELECT 'Greg','Pink' SELECT ColAFROM @tableaGROUP BY ColaHAVING MAX(CASE WHEN ColB = 'Red' THEN 1 ELSE 0 END ) + MAX(CASE WHEN ColB = 'Blue' THEN 1 ELSE 0 END) = 2JimEveryday I learn something that somebody else already knew |
 |
|
|
|
|
|
|
|