2015-04-02 39 views
0

我被授予此代码运行,但我仍然收到错误,即使我已经确定有正确数量的左/右括号。这是原始代码,因为我添加的括号似乎在错误的地方。与proc中的嵌套select语句问题sql

proc sql; 
    create table all as 
    select distinct a.id, a.count, b.date 
    from (select distinct id, count (*) as count from (select distinct id, p_id, date2 from table1) group by id) a 
(select distinct id, min(date2) as date format datetime. from table1) b 
where a.id=b.id; 
quit; 



(select distinct id, min(date2) as date format datetime. from 
        --------    - 
        22     22 
        202     76 
3520! table1) group by id) b 
ERROR 22-322: Syntax error, expecting one of the following:), ','. 

ERROR 202-322: The option or parameter is not recognized and will be ignored. 

ERROR 76-322: Syntax error, statement will be ignored. 

编辑:添加一个逗号后,然后我得到这个错误:

256  , (select id, min(date2) as date format datetime. from 
256! table1) group by id) b 
            - 
            22 
            76 
ERROR 22-322: Syntax error, expecting one of the following: ;, !, !!, &, (, *, **, +, ',', -, 
       '.', /, <, <=, <>, =, >, >=, ?, AND, BETWEEN, CONTAINS, EQ, EQT, EXCEPT, GE, GET, 
       GT, GTT, HAVING, IN, INTERSECT, IS, LE, LET, LIKE, LT, LTT, NE, NET, NOT, NOTIN, 
       OR, ORDER, OUTER, UNION, ^, ^=, |, ||, ~, ~=. 

ERROR 76-322: Syntax error, statement will be ignored. 

257 Where a.id=b.id; 
258 quit; 
+0

我thnk @sushil是正确的,是有缺失的逗号,但我也认为你需要在你的格式声明等号“FORMAT =日期时间。” – 2015-04-02 16:51:15

回答

1

的错误是没有括号的,但逗号(,)。您在第五行开头错过了逗号。

, (select distinct id, min(date2) as date format datetime. from table1) b 

编辑:缩进原代码与我的逗号修复。我不知道你为什么会得到这个新的错误。我复制了你的原始代码,并添加了逗号,并用虚拟数据测试了你的代码,并且它工作正常。我猜想一些隐藏的垃圾字符会导致错误。

data table1; 
input id p_id date2 :yymmdd10.; 
datalines; 
1 1 2012-01-15 
1 1 2012-01-15 
2 1 2012-01-15 
2 2 2012-01-15 
4 1 2012-01-15 
;;;; 
run; 
proc sql; 
    create table all as 
    select distinct a.id, a.count, b.date 
     from (select distinct id, count (*) as count 
       from (select distinct id, p_id, date2 from table1) 
      group by id 
      ) a 
    , (select distinct id, min(date2) as date format datetime. 
     from table1 
    ) b 
where a.id=b.id; 
quit; 
+0

我计算了2个左括号和3个右括号,不应该总是相等吗?我不确定,但因为我添加了逗号,它仍然给我错误。 – PinkyL 2015-04-02 17:44:39

+0

@PinkyL请检查编辑的答案。我缩进了你的原始代码,并添加了逗号修复,并测试了你的代码和虚拟数据,它工作的很好。 – sushil 2015-04-02 18:04:08

+0

我看到现在是什么问题,在我的原始编号中有一个额外的组,可能导致所有问题。我拿出来并将其格式化为您的示例,它正在工作......非常感谢。 – PinkyL 2015-04-02 18:12:32