|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Case in JoinMy problem is that I have 2 possible fields I want to join to the same table. If one is null, use the other. For example: Create Table Position ( UserID1 int, UserID2 int ) Create Table Logon ( UserID int, UserName ) I want to do something like (I know this doesn't work, but you should get the idea of what I am looking for from it). Select UserName From Position P Join on Logon L (CASE WHEN UserID1 is not null Then (P.UserID1 = L.UserID) ELSE (P.UserID2 = L.UserID) END) I am trying to get the User Name from UserID1, unless it is null. If that is the case, then get the User Name from UserID2. Thanks, Tom Select UserName From Position P Join Logon L
--- the join condition implies that UserID1 is not null on P.UserID1 = L.UserID union all Select UserName From Position P Join on Logon L on P.UserID2 = L.UserID where UserID1 is null The case statement will work, but the isnull function is better.
select isnull(columnA, columnB). If you have multiple columns to compare for nulls use coalesce. select coalesce(columnA,columnB,columnC....) "Gary Gibbs" <ggi***@aahs.org> wrote in message I tried to use the Case statement, but got an error on the "=" sign.news:1135974528.225930.160070@g44g2000cwa.googlegroups.com... > The case statement will work, but the isnull function is better. > select isnull(columnA, columnB). > I don't see how the isnull or coalesce.helps me. If I were looking for the > If you have multiple columns to compare for nulls use coalesce. select > coalesce(columnA,columnB,columnC....) IsNull from the data that would be fine, but in my case I have 2 UserID, which would be different records in the Logon table. I need to use a Join to get the correct UserName, I believe. Using the isnull would get me the correct UserID, but I still don't have the User Name for that UserID. Perhaps 2 separate joins. Thanks, Tom tshad (tscheider***@ftsolutions.com) writes:
> "Gary Gibbs" <ggi***@aahs.org> wrote in message CASE is not a statement in T-SQL, it is an expression. And just like> news:1135974528.225930.160070@g44g2000cwa.googlegroups.com... >> The case statement will work, but the isnull function is better. >> select isnull(columnA, columnB). > > I tried to use the Case statement, but got an error on the "=" sign. any other expression it returns a value. Thus what you had: Select UserName From Position P Join on Logon L (CASE WHEN UserID1 is not null Then (P.UserID1 = L.UserID) ELSE (P.UserID2 = L.UserID) END) Does not cut it, because 1) ON appears to eaarly, it should come after the table with its alias. 2) The JOIN operator is followed by a boolean expression, but CASE can never return boolean, because there is no boolean datatype in SQL. 3) And therefore the value of one branch in the CASE cannot be "P.UserID1 = L.UserID". This you can write: Select UserName From Position P Join Logon L ON L.UserUD = CASE WHEN P.UserID1 is not null Then P.UserID1 ELSE P.UserID2 END coalesce(P.UserID1, P.UserID2) is a short-hand notation for the same thing. -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx "Erland Sommarskog" <esq***@sommarskog.se> wrote in message That makes sense now.news:Xns973CF3D7CD071Yazorman@127.0.0.1... > tshad (tscheider***@ftsolutions.com) writes: > > "Gary Gibbs" <ggi***@aahs.org> wrote in message > > news:1135974528.225930.160070@g44g2000cwa.googlegroups.com... > >> The case statement will work, but the isnull function is better. > >> select isnull(columnA, columnB). > > > > I tried to use the Case statement, but got an error on the "=" sign. > > CASE is not a statement in T-SQL, it is an expression. And just like > any other expression it returns a value. Thus what you had: Show quote > That works great.> Select UserName > From Position P > Join on Logon L (CASE WHEN UserID1 is not null > Then (P.UserID1 = L.UserID) > ELSE (P.UserID2 = L.UserID) > END) > > Does not cut it, because > > 1) ON appears to eaarly, it should come after the table with its alias. > 2) The JOIN operator is followed by a boolean expression, but CASE > can never return boolean, because there is no boolean datatype in > SQL. > 3) And therefore the value of one branch in the CASE cannot be > "P.UserID1 = L.UserID". > > This you can write: > > Select UserName > From Position P > Join Logon L ON L.UserUD = CASE WHEN P.UserID1 is not null > Then P.UserID1 > ELSE P.UserID2 > END > This also would work. I misunderstood what Gary was saying.> coalesce(P.UserID1, P.UserID2) is a short-hand notation for the same > thing. Thanks, Tom Show quote > > > -- > Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se > > Books Online for SQL Server 2005 at > http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx > Books Online for SQL Server 2000 at > http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx tshad wrote:
> Can you put a Case statement in a Join? Of course...Show quote > Create Table Position Select UserName> ( > UserID1 int, > UserID2 int > ) > > Create Table Logon > ( > UserID int, > UserName > ) > > I want to do something like (I know this doesn't work, but you should get > the idea of what I am looking for from it). > > Select UserName From Position P Join on Logon L (CASE WHEN UserID1 is not > null Then (P.UserID1 = L.UserID) ELSE (P.UserID2 = L.UserID) END) From Position P Join Logon L on coalesce(P.UserID1, P.UserID2) = L.UserID Dieter Using Case:
Select UserName From Position P Join Logon L On L.UserId = Case When P.UserId1 Is Not Null Then P.UserId1 Else P.UserId2 End Using Coalesce: Select UserName From Position P Join Logon L On L.UserId = Coalesce(P.UserId1, P.UserId2) Using Isnull: Select UserName From Position P Join Logon L On L.UserId = Isnull(P.Userid1, P.UserId2) tshad wrote: Show quote > Can you put a Case statement in a Join? > > My problem is that I have 2 possible fields I want to join to the same > table. If one is null, use the other. > > For example: > > Create Table Position > ( > UserID1 int, > UserID2 int > ) > > Create Table Logon > ( > UserID int, > UserName > ) > > I want to do something like (I know this doesn't work, but you should get > the idea of what I am looking for from it). > > Select UserName From Position P Join on Logon L (CASE WHEN UserID1 is not > null Then (P.UserID1 = L.UserID) ELSE (P.UserID2 = L.UserID) END) > > I am trying to get the User Name from UserID1, unless it is null. If that > is the case, then get the User Name from UserID2. > > Thanks, > > Tom > > |
|||||||||||||||||||||||