简体   繁体   中英

SQLite Subquery Syntax Error when using Multiple Columns

It seems so simple; but I can't get this working.

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

This query gives me a syntax error. If I amend it to only use a single column (remove the MAX(QuoteTimewstamp)) it works.

It seems to be correct, as per the documentation.

Adding "AS" for the subquery alias makes no difference.

Here's the full error message from 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]

This seems to work although, to me, it seems counter-intuitive. It is returning "Symbol", "Bid" and "Ask" from the row with MAX(QuoteTimestamp).

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

I'm sure Oracle and their ilk would tell me that Symbol, Bid and Ask are not group-by expressions.

Thanks for all the help.

Thanks, Al.

In SQLite 3.7.11 or later, you do not need to use a subquery at all:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM