简体   繁体   English

选择带有子查询的案例选择并使用页首

[英]select Case with subqueries select and using Top

Hi i want to retrive the list of browsers used by user id 嗨,我想检索用户ID使用的浏览器列表

Table Contains 表格包含

UserID int UserID int

BrowserName nvarchar(40) 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 . 但是如何在此查询中仅选择前1个子字符串。

eg : after when in case statement in need only one row returned for that case, i tried this, but not getting idea how to implement in case 例如:当case语句仅需要返回该行的一行后,我尝试了此操作,但不知道如何实现case

THEN   select top 1 SUBSTRING(BrowserName,PatIndex('%IE%',BrowserName),8)  from browsertable

output will be like this 输出将像这样

IE IE浏览器

FIREFOX FIREFOX

CHROME

if the user used three browsers 如果用户使用了三个浏览器

Just add DISTINCT: 只需添加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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM