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 |
|
tmaiden
Yak Posting Veteran
86 Posts |
Posted - 2007-12-20 : 11:07:52
|
| I have a table which im joining that looks like this123,1123,2123,3123,4456,1456,18456,71456,99I want to return the first column #s where there isnt a 2 in the second column. and i need it as a group by...like, select column1 from table1 where column2 <> 2....but that above query would still grab the 123.. because of the fk..i would like the query to only pull the 456 b/c none of the fks with 456 have a 2... understand?thanks |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2007-12-20 : 11:12:35
|
| select * from tablewhere column 1 = 456 and column 2 <> 2 |
 |
|
|
tmaiden
Yak Posting Veteran
86 Posts |
Posted - 2007-12-20 : 11:14:13
|
| there are more rows in the table so i cant force in the 456 |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2007-12-20 : 11:17:10
|
Here is one way:create table #t (c1 int, c2 int)insert #tselect 123,1 union allselect 123,2 union allselect 123,3 union allselect 123,4 union allselect 456,1 union allselect 456,18 union allselect 456,71 union allselect 456,99select t.c1from #t tleft join ( select c1 from #t where c2 = 2 ) x on x.c1 = t.c1where x.c1 is nullgroup by t.c1output:c1 ----------- 456 Be One with the OptimizerTG |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2007-12-20 : 11:20:50
|
| SELECT DISTINCT Col1 FROM Table t1LEFT OUTER JOIN(SELECT DISTINCT Col1FROM TABLEWHERE Col2=2) t2ON t1.Col1=t2.Col1WHERE t2.Col1 IS NULL |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-20 : 11:39:40
|
Here is a real fast way to get teh records you want.Only 1 table scan also.DECLARE @Sample TABLE (Col1 INT, Col2 INT)INSERT @SampleSELECT 123, 1 UNION ALLSELECT 123, 2 UNION ALLSELECT 123, 3 UNION ALLSELECT 123, 4 UNION ALLSELECT 456, 1 UNION ALLSELECT 456, 18 UNION ALLSELECT 456, 71 UNION ALLSELECT 456, 99SELECT Col1FROM @SampleGROUP BY Col1HAVING MAX(CASE WHEN Col2 = 2 THEN 1 ELSE 0 END) = 0 Stats: The two other suggestions needs 27 reads. My suggestion 3 reads. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-12-21 : 01:36:37
|
Good one Peso. Other method isSELECT Col1FROM @SampleGROUP BY Col1HAVING COUNT(CASE WHEN Col2 = 2 THEN 1 END) = 0 MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|