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 |
|
tjbarr
Starting Member
8 Posts |
Posted - 2009-08-18 : 15:04:09
|
| Having trouble with a "simple" sqlSelect Distinct B.AddrIDfrom Table_AddressWhere AddrType in ('a','b','c') and addrlocatr <> 'g'Will give me records ID 01 and 04 belowI want a list of all id's that have no addrstatus ='g' regardless of the addrtype. So ID 01, 02 or 03 should not show up in my query but ID 04 Should. Table_AddressId addrtype addrstatus01 home g01 bus l02 home g03 bus g04 bus l |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2009-08-18 : 15:06:49
|
The shown query is not fitting to the shown sample column names.edit: and also the data is not fitting.Your select would return nothing. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
tjbarr
Starting Member
8 Posts |
Posted - 2009-08-18 : 15:10:51
|
| Posted - 08/18/2009 : 15:04:09 Show Profile Reply with QuoteHaving trouble with a "simple" sqlSelect Distinct AddrIDfrom Table_AddressWhere AddrType in ('a','b','c')and addrlocatr <> 'g'Will give me records ID 01 and 04 belowI want a list of all id's that have no addrlocatr ='g' regardless of the addrtype. So ID 01, 02 or 03 should not show up in my query but ID 04 Should.Table_AddressId addrtype addrlocatr01 a g01 b l02 a g03 b g04 b l |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-18 : 15:30:15
|
Here's one way:set nocount ondeclare @Table_Address table (Id int, addrtype char(1), addrlocatr char(1))insert @table_addressselect 01, 'a', 'g' union allselect 01, 'b', 'l' union allselect 02, 'a', 'g' union allselect 03, 'b', 'g' union allselect 04, 'b', 'l'select ta.idfrom @table_address taLeft outer join ( select id from @table_address where addrLocatr = 'g' group by id ) d on d.id = ta.idwhere d.id is nullgroup by ta.idoutput:id-----------4 EDIT:included sample data and outputBe One with the OptimizerTG |
 |
|
|
tjbarr
Starting Member
8 Posts |
Posted - 2009-08-18 : 15:38:33
|
| I understand the second part but the first part:set nocount ondeclare @Table_Address table (Id int, addrtype char(1), addrlocatr char(1))insert @table_addressselect 01, 'a', 'g' union allselect 01, 'b', 'l' union allselect 02, 'a', 'g' union allselect 03, 'b', 'g' union allselect 04, 'b', 'l'What if there are 50,000 records? |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-18 : 15:40:34
|
| <sigh>That is just some sample data in a table variable so that I could test my query. Just use the blue part but change the "@table_address" to your table name.Be One with the OptimizerTG |
 |
|
|
tjbarr
Starting Member
8 Posts |
Posted - 2009-08-18 : 16:01:33
|
| Yes I figured it out. Sorry to be such a "Newbie" Thanks |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-08-18 : 16:06:57
|
| No worries - we've all been thereBe One with the OptimizerTG |
 |
|
|
|
|
|
|
|