带别名的case语句

时间:2017-03-27 15:13:00

标签: sql-server case

希望在此脚本中使用case语句。第一次尝试它并且不确定它有什么问题。

SELECT DocumentDest.Keycode  AS [Acct #],
       Customer.Name,
       DocumentDest.Emails,
       Case
           Customer.Freq AS [Weekly or Monthly]
           When 'W' then 'Weekly'
           When 'M' then 'Monthly'
           Else 'None'
       End
FROM       DocumentDest
INNER JOIN Customer ON DocumentDest.Keycode = Customer.KeyCode
WHERE  (DocumentDest.Type = 'cus') AND (DocumentDest.[Document] IN ('stmt', 'wstmt'))

1 个答案:

答案 0 :(得分:0)

别名Weekly or Monthly不得出现在CASE语句的中间:这是一个SQL-Server版本,因此对于MySql表单别名。

SELECT 
      DocumentDest.Keycode AS [Acct #], 
      Customer.Name, 
      DocumentDest.Emails, 
      Case Customer.Freq When 'W' then 'Weekly' When 'M' then 'Monthly' Else 'None'      
      End AS [Weekly or Monthly] 
    FROM DocumentDest INNER JOIN Customer 
    ON DocumentDest.Keycode = Customer.KeyCode 
    WHERE DocumentDest.Type = 'cus' 
    AND DocumentDest[Document IN ('stmt', 'wstmt')