SQLite Subquery Syntax Error when using Multiple Columns - sqlite

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

Related

Oracle: SELECT to_number('3+11+7') FROM dual; gives ora-01722

I am using oracle 11g R2. When i query SELECT to_number('3+11+7') FROM dual. It gives ora-01722.
and when i remove single quotes, SELECT to_number(3+11+7) FROM dual. Then it gives answer 21.
I just removed single quotes from string. How can i do it with built in functions.
Thanks.
You could pass evaluation of the expression to XML:
select to_number(xmlquery('3+11+7' returning content)) from dual;
db<>fiddle

Selected non-aggregate values must be part of the associated group. SELECT Command Failed

I am using the below query and it gives error that "Selected non-aggregate values must be part of the associated group. SELECT Command Failed."
SELECT TOP 100 X_ISP_AFF_ADDR_SEQ, CAST(COUNT(*) AS BIGINT) AS COUNT_ROW FROM S_CONTACT WHERE X_ISP_AFF_ADDR_SEQ NOT LIKE '%[a-zA-Z]%';
I changed the above query as written below but the error still persists.
SELECT TOP 100 X_ISP_AFF_ADDR_SEQ, CAST(COUNT(X_ISP_AFF_ADDR_SEQ) AS BIGINT) AS COUNT_ROW FROM S_CONTACT WHERE X_ISP_AFF_ADDR_SEQ NOT LIKE '%[a-zA-Z]%';
You presumably are missing a GROUP BY clause here:
SELECT TOP 100
X_ISP_AFF_ADDR_SEQ,
CAST(COUNT(*) AS BIGINT) AS COUNT_ROW
FROM S_CONTACT
WHERE
X_ISP_AFF_ADDR_SEQ NOT LIKE '%[a-zA-Z]%'
GROUP BY
X_ISP_AFF_ADDR_SEQ;
The exact error you were seeing with your original query has to do with that selecting X_ISP_AFF_ADDR_SEQ instructs Teradata to return a value for each record in the table, whereas COUNT() returns a value over the entire table. It is not possible (in general) to mix aggregates and non aggregates in a select clause.

R with postgresql database

I've been trying to query data from postgresql database (pgadmin) into R and analyse. Most of the queries work except when I try to write a condition specifically to filter out most of the rows. Please find the code below
dbGetQuery(con, 'select * from "db_name"."User" where "db_name"."User"."FirstName" = "Mani" ')
Error in result_create(conn#ptr, statement) :
Failed to prepare query: ERROR: column "Mani" does not exist
LINE 1: ...from "db_name"."User" where "db_name"."User"."FirstName" = "Mani"
^
this is the error I get, Why is it considering Mani as a column when it is just an element. Someone pls assist me
String literals in Postgres (and most flavors of SQL) take single quotes. This, combined with a few other optimizations in your code leave us with this:
sql <- "select * from db_name.User u where u.FirstName = 'Mani'"
dbGetQuery(con, sql)
Note that introduced a table alias, for the User table, so that we don't have to repeat the fully qualified name in the WHERE clause.

Need help calculate 90th percentile

SELECT
session_start_dt
,PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY excess_wait)
OVER (PARTITION BY session_start_dt)
FROM Excess_Wait_AB
This query results is following error:
[Teradata Database] [TeraJDBC 15.10.00.22] [Error 3707] [SQLState
42000] Syntax error, expected something like a name or a Unicode
delimited identifier between ')' and the 'OVER' keyword
.
Try
SELECT
session_start_dt
, PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY excess_wait) as pcntl
FROM Excess_Wait_AB
This is another option:
SELECT
session_start_dt
, PERCENTILE_DISC(0.9) WITHIN GROUP (ORDER BY excess_wait) as pcntl
FROM Excess_Wait_AB
Difference between them explained here.

Oracle: Error when executing query

I have a table SyncTokenLock that has column lockName that is of CLOB type. When I run following query from SQLDeveloper -
select * from SyncTokenLock where
lockName='com.vmware.horizon.datastore.impl.ProvisioningStateDataServiceImpl';
I get following exception -
ORA-00932: inconsistent datatypes: expected - got CLOB
00932. 00000 - "inconsistent datatypes: expected %s got %s"
I get similar error when this query is executed through Hibernate (3.6.10) against Oracle 11g. Hibernate throws following exception -
ORA-00932: inconsistent datatypes: expected - got CLOB
Any idea what could the reason be.
Correct, you can't use equality with a CLOB in the WHERE clause. But you can do this:
SELECT * FROM SyncTokenLock
WHERE dbms_lob.substr(lockName, 100) =
'com.vmware.horizon.datastore.impl.ProvisioningStateDataServiceImpl';
Does your column really need to be a CLOB? Are you expecting values over 4000 characters? If not, use a VARCHAR2.
Instead of using the equal sign, you may use like:
select * from SyncTokenLock where lockName like 'com.vmware.horizon.datastore.impl.ProvisioningStateDataServiceImpl';

Resources