oracle SQL过程错误未正确结束

时间:2017-09-18 08:43:23

标签: oracle

我正在尝试使用以下字母m,f,i,c选择列paxtype中每个值的总数,但我的错误是sql未正确结束

(select b.PAXTYPE from xxxx b, xxx a)
(case b.PAXTYPE
    when 'M' then count('M')
    when 'F' then count('F')
    when 'I' then count('I')
    when 'C' then count('C')
    END)
where a.date_key=to_char(b.FLIGHTDATE,'RRRRMMDD')
and a.FLTNUM_KEY= trim(substr(b.flightnumber,3))
and a.origin=b.frm
and a.destination=b.too
--and a.date_key=20170801
--and fightnumber = '100'
and trim(a.cancelled) is null
and rownum = 1
)

1 个答案:

答案 0 :(得分:0)

首先,您没有使用正确的sql语法。此外,您的查询还有其他几个问题。

  • 表名应位于select
  • 中的列之后
  • 您需要使用group by,因为您需要count
  • 无需CASE阻止
  • 为什么需要ROWNUM = 1

    此查询应该可以满足您的要求。

    SELECT b.PAXTYPE, COUNT (b.PAXTYPE)
              FROM xxxx b, xxx a
             WHERE     a.date_key = TO_CHAR (b.FLIGHTDATE, 'RRRRMMDD')
                   AND a.FLTNUM_KEY = TRIM (SUBSTR (b.flightnumber, 3))
                   AND a.origin = b.frm
                   AND a.destination = b.too
                   --and a.date_key=20170801
                   --and fightnumber = '100'
                   AND TRIM (a.cancelled) IS NULL
          --and rownum = 1  # Why was it required?
          GROUP BY b.PAXTYPE;
    

此外,如果您只需要M,F,I,C的计数,请在AND b.PAXTYPE IN ('M','F','I','C')之前添加group by