使用多列时SQLite子查询语法错误

时间:2015-03-31 06:09:24

标签: sqlite group-by subquery syntax-error multiple-columns

看起来很简单;但我无法做到这一点。

select ISIN, QuoteTimestamp
from QuoteData
where (ISIN, QuoteTimestamp) IN
      (select ISIN, MAX(QuoteTimestamp) QuoteTimestamp
       from QuoteData
       group by ISIN)

此查询给出了语法错误。如果我修改它只使用一个列(删除MAX(QuoteTimewstamp))它可以工作。

根据文档,这似乎是正确的。

添加" AS"对于子查询别名没有任何区别。

这是来自SQLite Manager的完整错误消息。

SQLiteManager: Likely SQL syntax error: select ISIN, QuoteTimestamp
from QuoteData
where (ISIN, QuoteTimestamp) IN
       (select ISIN, MAX(QuoteTimestamp) AS QuoteTimestamp
        from QuoteData
        group by ISIN)

 [ near ",": syntax error ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]

这似乎有效,但对我来说,这似乎是违反直觉的。它正在返回" Symbol"," Bid"和"问"来自MAX(QuoteTimestamp)的行。

select ISIN, Symbol, Bid, Ask, MAX(QuoteTimestamp)
from QuoteData
group by ISIN

我确信甲骨文和他们的同事会告诉我,Symbol,Bid和Ask不是分组表达式。

感谢您的帮助。

谢谢, 人

1 个答案:

答案 0 :(得分:0)

在SQLite 3.7.11或更高版本中,您根本不需要使用子查询:

select ISIN, MAX(QuoteTimestamp) QuoteTimestamp
from QuoteData
group by ISIN