只对 co=x 的纪录感兴趣
就在where 里写
where co='x'

解决方案 »

  1.   

    不明白,这为什么用case when
    如楼上说的,如果你只要col='X' 的,就放在 where 后面啊
      

  2.   

    select a
    = case 
    when co1=‘x’,
    then (...)
    请问where 是不是加在括号里?如果加在括号里只能对当co1=‘x'时的结果起作用,整个select的结果集还是会包含别的结果的。
      

  3.   

    我把查询简化了,then后面有自身的联接和top语句,太长了,我没有贴出来。
      

  4.   

    1.首先CASE用得不太對:
      應該 Select a=case col where 'x' then ... else ... end
    2.在這裏使用CASE對於效率沒有任何的幫助,因為CASE是加在每一條上的,並不能實際減少記錄量.
      可以這樣處理 select ... from (select * from t where col='x') t,這樣就行,如果你的SQL很簡單的話也可以不用這種嵌套,直接將條件放在WHERE子句中
      

  5.   

    USE AdventureWorks;
    GO
    SELECT   ProductNumber, ProductLine =
          CASE 
             WHEN ProductLine = 'R' THEN 'Road'
             WHEN ProductLine = 'M' THEN 'Mountain'
             WHEN ProductLine = 'T' THEN 'Touring'
             WHEN ProductLine = 'S' THEN 'Other sale items'
             ELSE 'Not for sale'
          END,
       Name
    FROM Production.Product
    ORDER BY ProductNumber;
    -----------------------------
    ---同下面的查询结果是一样的
    GO
    SELECT   ProductNumber, Category =
          CASE ProductLine
             WHEN 'R' THEN 'Road'
             WHEN 'M' THEN 'Mountain'
             WHEN 'T' THEN 'Touring'
             WHEN 'S' THEN 'Other sale items'
             ELSE 'Not for sale'
          END,
       Name
    FROM Production.Product
    ORDER BY ProductNumber;
    GO所以
    select a
    = case 
    when co1=‘x’,
    then .....语法没错。
    select a
    = case 
    when co1=‘x’,
    then (...)请问where 是不是加在括号里?如果加在括号里只能对当co1=‘x'时的结果起作用,整个select的结果集还是会包含别的结果的。(包含‘x'以外的纪录)
    有时候为了提高性能,想只得到’x'的纪录,
    这样的case 语句怎样写?
      

  6.   

    SELECT   ProductNumber, Category = 'Road',Name
    FROM Production.Product where ProductLine ='R'
    ORDER BY ProductNumber;