Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SELECTING A CASE VALUE ???


SELECTING A CASE VALUE ???

Author
Message
jbalbo
jbalbo
SSC-Addicted
SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)SSC-Addicted (405 reputation)

Group: General Forum Members
Points: 405 Visits: 668
Hi

I am using the following to retrive a clients "secure" name if alias-type of 004 exists.
Problem I have is if the client has more than one alias type(001,002, 003) they all show up
What I need to do is if a client has an alias code of 004 then use that no matter how many they have
if they don't have an oo4 and have multiple say 003's then use the client name ..
Thanks

SELECT CASE
WHEN at.CODE = '004'
THEN a.FName + ' ' + isnull(a.MName, '') + ' ' + + a.lname
ELSE c.FName + ' ' + ISNULL(c.MName, '') + ' ' + c.LName
END AS Tag_Data
FROM Client AS C
LEFT JOIN
-- Note the order of the clausing here. It causes an
-- inner join to the rest of the tables BEFORE the
-- outer join is included.
Client_To_Alias_Collection AS AC
INNER JOIN Alias AS A
ON AC.OID_Link = A.OID
INNER JOIN Alias_Type AS AT
ON AT.OID = A.Alias_Type_Moniker
ON c.oid = ac.oid
AND A.EXPDate IS NULL WHERE c.OID = '12345'
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16566 Visits: 17016
Not much in the way of details to work with here. I took a shot in the dark. See if this gets you close.


select * from
(
select ROW_NUMBER() over(partition by c.ClientID order by case when at.CODE = '004' then 1 else 0 end) as RowNum,
CASE
WHEN at.CODE = '004'
THEN a.FName + ' ' + isnull(a.MName, '') + ' ' + + a.lname
ELSE c.FName + ' ' + ISNULL(c.MName, '') + ' ' + c.LName
END AS Tag_Data
FROM Client AS C
LEFT JOIN
-- Note the order of the clausing here. It causes an
-- inner join to the rest of the tables BEFORE the
-- outer join is included.
Client_To_Alias_Collection AS AC
INNER JOIN Alias AS A
ON AC.OID_Link = A.OID
INNER JOIN Alias_Type AS AT
ON AT.OID = A.Alias_Type_Moniker
ON c.oid = ac.oid
AND A.EXPDate IS NULL WHERE c.OID = '12345'
) x
where x.RowNum = 1



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search