select Case with subqueries select and using Top
Hi i want to retrive the list of browsers used by user id
Table Contains
UserID int
BrowserName nvarchar(40)
here is my Query
select browser =
CASE
WHEN ( PATINDEX('%IE%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%IE%',BrowserName),8)
WHEN (PATINDEX('%Firefox%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%Firefox%',BrowserName),8)
WHEN (PATINDEX('%Chrome%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%Chrome%',BrowserName),6)
END
from tableBrowsers where UserId =21
But how to select only top 1 substring in this query .
eg : after when in case statement i n need only one row returned for that case, i tried this, but not getting idea how to implement in case
THEN select top 1 SUBSTRING(BrowserName,PatIndex('%IE%',BrowserName),8) from browsertable
out开发者_高级运维put will be like this
IE
FIREFOX
CHROME
if the user used three browsers
Just add DISTINCT:
SELECT DISTINCT browser =
CASE
WHEN ( PATINDEX('%IE%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%IE%',BrowserName),8)
WHEN (PATINDEX('%Firefox%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%Firefox%',BrowserName),8)
WHEN (PATINDEX('%Chrome%',BrowserName) IS not null) THEN SUBSTRING(BrowserName,PatIndex('%Chrome%',BrowserName),6)
END
FROM tableBrowsers where UserId =21
精彩评论