Prev: Installation Problem SQL Server 2005 + Windows 7
Next: Return valuse based on multiple "if exists' conditions (Advanced)
From: Plamen Ratchev on 6 Feb 2010 18:51 SELECT TOP 1 WITH TIES source, price, state FROM #TempTable WHERE source IN ('Tiger', 'Ape', 'Elephant') ORDER BY CASE source WHEN 'Tiger' THEN 1 WHEN 'Ape' THEN 2 WHEN 'Elephant' THEN 3 END; -- Plamen Ratchev http://www.SQLStudio.com
From: SQL Learner on 6 Feb 2010 21:09 Plamen, This is cool. Thanks. Could you or anyone explain how the following code work? ORDER BY CASE source WHEN 'Tiger' THEN 1 WHEN 'Ape' THEN 2 WHEN 'Elephant' THEN 3 END; Thanks again. Anthony
From: Plamen Ratchev on 6 Feb 2010 22:23 The CASE expression does mapping of the sources to sort values, in a way giving priority to 1, then 2, 3, etc. That way the source with sort value 1 (Tiger) is on top of the list if any entries exist. If not then the next one. -- Plamen Ratchev http://www.SQLStudio.com
From: SQL Learner on 7 Feb 2010 19:45
Thanks again,Plamen! SQL Learner |