带有和SAS的SQL select语句

时间:2013-07-22 19:49:40

标签: sql sas

我试图用嵌套的and编写一个SQL select语句来获取一些数据:

PROC SQL;

CREATE TABLE WORK.TEMP AS

SELECT DISTINCT

(CASE WHEN t1.TYPE_OF_SALE = 'Cake' AND t1.ADD_ON_FLAG = 0 THEN 'Arrangements'

WHEN t1.TYPE_OF_SALE = 'Cake' AND t1.ADD_ON_FLAG = 1 THEN 'Add-ons'

ELSE 'Direct' END) SALE_TYPE, #ERROR IN THIS LINE


(t1.UNIT_PRICE+t1.SERVICE_CHARGE_AMT) AS TOTAL_VAL

FROM WORK.FB_ORDERS t1

ORDER BY SALE_TYPE;

QUIT;

我出于某种原因在第一行中出错,是否无法将andcase statement一起使用?

Error Message:
Error 22-322: Syntax error, expecting one of the following: !,!!,&...

此错误存在于case statement

的末尾

2 个答案:

答案 0 :(得分:5)

我认为问题在于SAS需要as来定义列别名:

PROC SQL;
CREATE TABLE WORK.TEMP AS
    SELECT DISTINCT (CASE WHEN t1.TYPE_OF_SALE = 'Cake' AND t1.ADD_ON_FLAG = 0 THEN 'Arrangements'
                          WHEN t1.TYPE_OF_SALE = 'Cake' AND t1.ADD_ON_FLAG = 1 THEN 'Add-ons'
                          ELSE 'Direct'
                      END) as SALE_TYPE,
                    (t1.UNIT_PRICE+t1.SERVICE_CHARGE_AMT) AS TOTAL_VAL
    FROM WORK.FB_ORDERS t1
    ORDER BY SALE_TYPE;

QUIT;

SAS允许列之后的其他表达式,例如FORMAT,所以我认为需要as来防止表达式中出现歧义。

答案 1 :(得分:2)

你需要一个AS:

proc sql;
select distinct
(case when age<14 and sex='F' then "Young Girl"
      when age<14 and sex='M' then "Young Boy"
      else "Other" end) as type from sashelp.class;
      quit;