0

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.

4
  • Hi,I get the same result when using "AS". Here's the full error. 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] Commented Mar 31, 2015 at 6:41
  • Please add the message to your question using the edit function. Commented Mar 31, 2015 at 6:48
  • Please show the part of the documentation that allows multi-valued expressions. Commented Mar 31, 2015 at 7:29
  • OK. I must have assumed that you could specify multiple columns as you can with other DBs. Thanks for the comments. Commented Mar 31, 2015 at 7:39

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

1 Comment

Interesting. I did not think I could do this as I wanted to select other columns, and for those to be from the row with MAX(QuoteTimestamp) and I thought it would complain about non-grouping expressions. I shall have a play around with this. I hadn't shown my other columns, for brevity. Now I understand that you can't specify multiple columns, all becomes more clear. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.