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 |
suchiate
Starting Member
33 Posts |
Posted - 2007-03-14 : 06:29:16
|
Hey guys,I have a problem with selecting multiple values using the Except statement. I have two tables, one is a lookup table and another one is the actual data. I want to select the data from my lookup which does not exist in my actual data.Something likeSelect GroupID, GroupDesc from Lookup EXCEPT (Select GroupID from tblData where Field = Condition)I wan to display the Description as well... how can i do it?Thanks alot in advance, hope to hear from you guys soon. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-14 : 06:35:28
|
You must use SQL Server 2005 to use EXCEPT keyword.In SQL Server 2000 you can use LEFT JOINSELECT GroupID, GroupDescFROM LookupLEFT JOIN tblData ON tblData.GroupID = Lookup.GroupID AND tblData.Field = ConditionWHERE tblData.GroupID IS NULL You can also play around with NOT EXISTS and NOT IN if you want to.Peter LarssonHelsingborg, Sweden |
 |
|
suchiate
Starting Member
33 Posts |
Posted - 2007-03-14 : 06:40:39
|
yea i am using SQL 2005 Express,and thanks alot it is working now with your previous post reference =)can you teach me how it would look like if i were to use Except to do the same thing? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-14 : 06:44:08
|
[code]SELECT GroupIDFROM LookupEXCEPTSELECT GroupIDFROM tblData WHERE Field = Condition[/code]Peter LarssonHelsingborg, Sweden |
 |
|
suchiate
Starting Member
33 Posts |
Posted - 2007-03-14 : 06:50:03
|
Yea that's what I posted initially, i mean what would the except look like if i wanted the Description out as well? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-14 : 06:53:38
|
[code]SELECT x.GroupID, y.GroupDescFROM ( SELECT GroupID FROM Lookup EXCEPT SELECT GroupID FROM tblData WHERE Field = Condition ) AS xINNER JOIN Lookup AS y ON y.GroupID = x.GroupID[/code]Peter LarssonHelsingborg, SwedenPeter LarssonHelsingborg, Sweden |
 |
|
suchiate
Starting Member
33 Posts |
Posted - 2007-03-14 : 06:58:43
|
Wow!Thanks alot, in terms of performance-wise,LEFT join will be better in this case right? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-14 : 07:07:25
|
It might.It depends if there are duplicates in the tblData table.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|