按自定义列筛选SQL

时间:2014-09-02 18:53:40

标签: sql-server filtering case

我想知道我是否可以根据我的" Style"轻松过滤我的结果。我的查询中的列。

select distinct
  m.ManagerName, 
  p.ProductName, 
  p.slocumrank,
  case 
    when s2.SubType2ID = 45 then 'Large Cap' 
    else s2.SubType2Name 
  End + ' ' + s1.SubType1Name as 'Style'
from QuantPerformance qp
where Style = 'ABCD'

目前,我的where语句会过滤掉所有内容。

1 个答案:

答案 0 :(得分:2)

SELECT * FROM 
(
SELECT DISTINCT  ManagerName
               , ProductName
               , slocumrank
               , case when SubType2ID = 45 
                      then 'Large Cap' 
                      else  SubType2Name 
                 End + ' ' + SubType1Name  AS  [Style]
from QuantPerformance
 ) A
where A.Style = 'ABCD'

或者

SELECT DISTINCT  ManagerName
               , ProductName
               , slocumrank
               , case when SubType2ID = 45 
                      then 'Large Cap' 
                      else  SubType2Name 
                 End + ' ' + SubType1Name  AS  [Style]
from QuantPerformance
where case when SubType2ID = 45 
           then 'Large Cap' 
           else  SubType2Name 
      End + ' ' + SubType1Name = 'ABCD'