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 |
|
ATG
Starting Member
35 Posts |
Posted - 2011-10-03 : 17:47:01
|
| Lets say I have a table with several entries. A name column and a car column. I want to be able to Select all people who own a car that is not a Toyota. NAME CARBob ToyotaBob FordRon Chevy Ben KiaI would want it to return Ron Chevy Ben KiaBecause Bob owns a Toyota even though he has a Ford too. How could I exclude all his records based on the one that contains "Toyota"?Thanks |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-10-03 : 17:57:05
|
| SELECT NAME, CAR FROM myTable AWHERE NOT EXISTS(SELECT * FROM myTable WHERE NAME=A.NAME AND CAR='Toyota') |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-04 : 00:37:39
|
| [code]SELECT Name,CarFROM(SELECT Name,CAR,SUM(case when CAR='Toyota' THEN 1 ELSE 0 END) OVER(PARTITION BY Name) AS ExclFROM table)tWHERE Excl=0[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
m_imran18
Starting Member
14 Posts |
Posted - 2011-10-04 : 02:25:37
|
| Drop Table testgoCreate table test([Name] nvarchar(50),[Car] nvarchar(50))GOinsert into test Select 'Bob' ,'Toyota'UnionSelect 'Bob', 'Ford'UnionSelect 'Ron', 'Chevy'UnionSelect 'Ben','Kia'GOSelect * from test Where [name] Not In (Select [name] from test Where [Car]='toyota') |
 |
|
|
|
|
|