MS-SQL Case when

显示时间:

select   getdate()   as   日期,case   month(getdate())   
  when   11   then   '十一'   
  when   12   then   '十二'   
  else   substring('一二三四五六七八九十',   month(getdate()),1)   
  end+'月'   as   月份

  

GROUP BY 子句中的 CASE when

SELECT 'Number of Titles', Count(*) 
FROM titles 
GROUP BY 
    CASE 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END 
GO 

 

  ORDER BY 的Case when

SELECT 
    CASE 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END AS Range, 
     Title 
FROM titles 
GROUP BY 
    CASE 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END, 
     Title 
ORDER BY 
    CASE 
        WHEN price IS NULL THEN 'Unpriced' 
        WHEN price < 10 THEN 'Bargain' 
        WHEN price BETWEEN 10 and 20 THEN 'Average' 
        ELSE 'Gift to impress relatives' 
    END, 
     Title 
GO  

 

你可能感兴趣的:(sql,Go)