R sqldf: Error table already in test - r

I tested the code before and it didn't give me any errors.
Now there is an error "sqldf: table T1 already in test".
How can I fix it?
ret <- sqldf("SELECT T1.*, T2.score FROM T1 JOIN T2 USING(date)")

You just need to execute:
sqldf("drop table test.T1")

You may have a dataframe with the same name as your database table. Clear your environment and try again.

Related

Join works in Azure SQL but fails with with R DBI connection: The multi-part identifier could not be found

I have a query that works perfectly in SSMS. But when running the query in R using the DBI package, I receive several multipart identifier errors: The multi-part identifier: "rt.secondary_id" could not be bound, "rt.third_id" could not be bound, and "t2.important" could not be bound.
select t1.[main_id]
,rt.secondary_id
,rt.third_id
,t1.[date_col]
,t2.important
from t1
inner join rt on t1.main_id = rt.main_id
inner join t2 on rt.main_id = t2.main_id
inner join (select t1.main_id, max(t1.date_col) as upload_time from t1 group by t1.main_id) AS ag ON t1.main_id = ag.main_id AND t1.date_col = ag.upload_time
The unique identifier in t1 is the combination of main_id and date_col, and this query finds the most recent entry in t1 for a given main_id.
Not exactly sure if my query is structured in a poor way or this is an R issue. I've tried adding SET NOCOUNT ON to the query based on what I thought might be related issues elsewhere on stackoverflow, but no dice.
I found out what my issue was- silly (but time consuming) mistake on my part... but essentially, I was bringing my SQL query into R via paste(scan(...), collapse = " "). I had a comment in my SQL query, --, which could not be read correctly by R. Deleting the comment OR switching the comment to /* ... */ syntax fixes the problem.

R odbc on oracle 11.2 ORA-00933

I am using the R DBI package to query a oracle database.
When using inner joins it gives me the following error.
Plain SELECTS without INNER JOIN works fine. Why?
Error: nanodbc/nanodbc.cpp:1617: 00000: [Oracle][ODBC][Ora]ORA-00933: SQL command not properly ended
DBI::dbGetQuery(con_ora, "SELECT dat.LP_ZP_ID, dat.LP_TIMESTAMP_LOC, dat.LP_WERT, zp.ZP_ZAEHLPUNKT
FROM EDM_LP_DATEN dat
INNER_JOIN EDM_ZAEHLPUNKT zp ON zp.ZP_ID=dat.LP_ZP_ID
WHERE dat.ZP_ID = 111 AND dat.LP_TIMESTAMP_LOC >= TIMESTAMP '2021-01-01 00:00:00'")
What's obviously wrong is this:
INNER_JOIN EDM_ZAEHLPUNKT
It is not inner_join but inner join (without underscore).
The rest of your query looks OK.

Update with multiple tables and query optimization

I have this update statement
UPDATE
pr
SET
pr.ult_prezzo_euro = ROUND(pr.ult_prezzo/fs.cambio,7)
FROM --error SQL COMMAND NOT PROPERLY ENDED
fin_prodotto prod INNER JOIN
fin_prodotto_linea fpl ON prod.prodotto_id=fpl.prodotto_id INNER JOIN
fin_att_fin faf ON fpl.attivita_fin_id=faf.attivita_fin_id INNER JOIN
fin_prezzo pr ON pr.attivita_fin_id=faf.attivita_fin_id INNER JOIN
flx_sec_posizione_dt_upd fs ON pr.attivita_fin_id=fs.attivita_fin_id
where
prod.prodotto_id=43
and faf.codice_titolo_cad_s is not null
and pr.ult_prezzo = pr.ult_prezzo_euro
and faf.divisa_quot_t<>'242'
and prod.gstd_esist_b='S'
and fpl.gstd_esist_b='S'
and faf.gstd_esist_b='S'
and pr.gstd_esist_b='S'
and pr.gstd_ult_user_s in ('AGGIORNAMENTO_POSIZIONE')
and pr.ult_prezzo>0
and fs.cambio>0;
it give me "SQL COMMAND NOT PROPERLY ENDED" pointing at FROM row,
And also i would optimize this update statement because this should run on about 2 millions records, how is possible?
i can't be sure it will be right answer without seeing your database structure, but first:
you have wrong syntax, it should look like this:
UPDATE fin_prezzo as pr SET pr.ult_prezzo_euro = ...
and then you will have to change chain of JOINS, to adequately join from fin_prezzo table (as the first table mentioned).
if it would be problem, you can 'UPDATE' another table in the statement (without changing/adding/removing updated columns)
Ad optimization part:
it will be good to start with SELECT statement (instead of UPDATE) to see, how it is joinning (maybe duplicating rows because of joins) and reduce the duplications, or not necessary joins.
secondly is quicker to move 'rules' from WHERE statement to JOIN statement
for example:
INNER JOIN fin_att_fin as faf ON fpl.attivita_fin_id=faf.attivita_fin_id AND faf.gstd_esist_b='S'
another good technique is comparing numbers instead of strings ('S', '242'...), it is simply quicker
rest is up to trying..
hope i helped a bit ;)

Same query, different results. Possible causes?

For testing purposes, I am querying the same table from the same database using two different GUIs (RStudio and SquirreLSQL).
The query in the SquirreLSQL console looks like this:
select count(distinct idstr) from fact_table where date::date='2014-10-30' and (w>0 or x>0 or y>0)
And in RStudio, I have the following code:
library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv,"databaseconnectionstring",user ="usr",password ="pwd",dbname = "db")
res <- dbSendQuery(con, "select count(distinct idstr) from fact_table where date::date='2014-10-30' and (w>0 or x>0 or y>0)")
The query done in SquirreLSQL returns almost twice as many rows as the one done in RStudio. What could cause the same exact query to return different values? The table and contents do not change.
Thanks to Jakub's response, I realized that the GUIs were in different timezones. To fix this, I ran the following line of SQL in SquirreLSQL to find the correct timezone:
SELECT current_setting('TIMEZONE')
It returned "America/New_York", so I then ran the following line in R to get the two programs in the same timezone:
dbGetQuery(con, "SET TIMEZONE TO 'America/New_York'")

I am working vbscript in HTA. Using SQLite for database need. And SQLite doesn't recognize equal to an integer

The target field is pk_2013_1qrt and its type is integer. But when i run the query below, results includes pk_2013_1qrt=1 values too.
Where did i go wrong?
Regards
SELECT *,ims_2012_modifiye.id AS imsID, ims_2012_modifiye.ilac_id AS ilacID,
ims_2012_modifiye.ilac_adi_duzgun AS iaDuzgun
FROM ims_2012_modifiye
LEFT JOIN ilaclar ON ims_2012_modifiye.ilac_id=ilaclar.id
WHERE ims_2012_modifiye.ilac_id NOTNULL
AND ims_2012_modifiye.asil_listede_mi=1
AND ims_2012_modifiye.ilac_id NOT IN
(SELECT ilac_id FROM piyasa_kontrol
WHERE pk_yil IN (2012,2011,2010,1900,1901))
OR
ims_2012_modifiye.oncelik_verilsin_mi=1
AND ims_2012_modifiye.pk_2013_1qrt=0
LIMIT 5
OR has lesser priority than AND. What you want is unclear, but the latest AND ims_2012_modifiye.pk_2013_1qrt=0 is applied only to the rows with ims_2012_modifiye.oncelik_verilsin_mi=1. Maybe this:
SELECT *,ims_2012_modifiye.id AS imsID, ims_2012_modifiye.ilac_id AS ilacID,
ims_2012_modifiye.ilac_adi_duzgun AS iaDuzgun
FROM ims_2012_modifiye
LEFT JOIN ilaclar ON ims_2012_modifiye.ilac_id=ilaclar.id
WHERE (ims_2012_modifiye.ilac_id NOTNULL
AND ims_2012_modifiye.asil_listede_mi=1
AND ims_2012_modifiye.ilac_id NOT IN
(SELECT ilac_id FROM piyasa_kontrol
WHERE pk_yil IN (2012,2011,2010,1900,1901))
OR
ims_2012_modifiye.oncelik_verilsin_mi=1)
AND ims_2012_modifiye.pk_2013_1qrt=0
LIMIT 5
Problem solved. As #Anton said AND has prior accessibility to query results. Move AND ims_2012_modifiye.pk_2013_1qrt=0 part just after to WHERE solves my problem. Thank you Anton, Regards

Resources