sqldf gives error while using subquery - mysql

I'm trying to get moving average by weekday, for that I'm using sql query.
Dataframe is
and sqldf code:
ma_782 = sqldf("SELECT
t1.Id_indicator, t1.Hour,
(
select SUM(t2.Value) / COUNT(t2.Value)
FROM max_value_782 AS t2
WHERE
t1.Hour = t2.Hour and
weekdays.Date(t1.Date) = weekdays.Date(t2.Date)
and DATEDIFF(t1.Date, t2.Date) BETWEEN 1 AND 42
) AS 'MA_by_weekday'
FROM max_value_782 AS t1 ;")
This gives error
Error in rsqlite_send_query(conn#ptr, statement) : near "(": syntax error
while it works from simple select:
sqldf("select * from max_value_782")

Consider replacing the weekdays. method as this syntax assumes a table qualifier. Since by default sqldf uses the SQLite dialect, use strftime to compare weekdays. Also, single quotes are used for string literals and not to enclose table/field identifiers. SQLite can uses brackets, backticks, or double quotes, or none if reserved words/special characters are not used.
ma_782 = sqldf("SELECT t1.Id_indicator, t1.Hour,
(SELECT AVG(t2.Value)
FROM max_value_782 AS t2
WHERE t1.Hour = t2.Hour
AND strftime('%w', t1.Date) = strftime('%w', t2.Date)
AND (t2.Date - t2.Date) BETWEEN 1 AND 42
) AS MA_by_weekday
FROM max_value_782 AS t1;")

Related

partial string returned as field value mysql

I am getting incomplete string value from mysql query result. It appears that when the string is too long, the value is sliced. I am fetching the value as a JSON string. There is no issue with my local database, no matter how long the field value is.
Here's my query:
CREATE DEFINER=`devdbuser`#`localhost` PROCEDURE `GetTrainingFilesByID`(IN trainingID int)
BEGIN
SELECT t1.*, concat(t2.firstname,' ',t2.lastname) as username
FROM users t2
INNER JOIN
(
SELECT t.*,
CONCAT('[',
GROUP_CONCAT(CONCAT('{"id":"',d.id,
'","name":"', d.title,
'","file_path":"',d.doc_path,
'","is_video":"',d.is_video,'"}'
)), ']'
) files
FROM training t
LEFT JOIN training_documents d
ON t.id = d.training_id
GROUP BY t.id
) t1
ON t1.updated_by = t2.id
WHERE t1.id = trainingID
LIMIT 1;
END
The issue is with files field value.
Here's the incomplete output:
"[{"id":"1", "name":"Branch Workflow Model for GBD_1.pdf", "file_path":"1455443689.pdf", "is_video":"0"},
{"id":"2", "name":"http://192.168.11.32/GBD-Videos/testvideo.mp4", "file_path":"http://192.168.11.32/GBD-Videos/testvideo.mp4", "is_video":"1"},
{"id":"6", "name":"COD-CC-CrowdWisdom-Report-exclusive_mybigcommerce_com-2018_09_26-05_28_58.pdf", "file_path":"1862665875.pdf", "is_video":"0"},
{"id":"7", "name":"https://www.youtube.com/watch?v=rCAIY5n1hPA.mp4", "file_path":"https://www.youtube.com/watch?v=rCAIY5n1hPA.mp4", "is_video":"1"},
{"id":"8", "name":"https://www.youtube.com/watch?v=rCAIY5n1hP1.mp4", "file_path":"https://www.youtube.com/watch?v=rCAIY5n1hP1.mp4", "is_video":"1"},
{"id":"19", "name":"Branch Workflow Model for GBD.docx", "file_path":"1250685453.docx", "is_video":"0"},
{"id":"20", "name":"COK_CorporateMedicineDelivery_Growbydata.pdf", "file_path":"1245383653.pdf", "is_video":"0"},
{"id":"21", "name":"COK_Intro_Growbydata.pdf", "file_path":"1918218679.pdf", "is_video":"0"},
{"id":"22", "name":"http:/]"
Does mysql have some feature that prevents long field result values? How do I fix this?
Its main because you are using group concat. i recommend you to run this query so that it will increase the limit of group concat
SET GLOBAL group_concat_max_len=15000
hope this helps. :)

SQL nested queries and subqueries

i've been having an error with this query and i'm not sure how to fix it. this query is supposed to filter occupations stored in my database to match the volunteer's occupation. please help me fix my query. all the names in this query are correctly spelled, i double checked all the spellings before writing the query here.
the error says "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT (SELECT count(*) FROM occupation_event WHERE event_id='8' AND occupationN' at line 1"
SELECT
*
FROM
volunteer_details
WHERE
user_id=73
AND
volunteer_occupation in (
SELECT
occupationName
FROM
occupation_event
WHERE
event_id=8
OR SELECT (
SELECT
count(*)
FROM
occupation_event
WHERE
event_id='8'
AND
occupationName = 'No Occupation Required') > 0 AS need
)
I think the error is the as need at the end. I would write this as:
SELECT vd.*
FROM volunteer_details vd
WHERE user_id = 73 AND
(vd.volunteer_occupation in (SELECT oe.occupationName FROM occupation_event oe WHERE oe event_id = 8) or
exists (select 1 from occupation_event oe where event_id = 8 and oe.occupationName = 'No Occupation Required')
);

Correct syntax to use in mysql 5.5.25 for not exists

Hi I'm trying to write a cursor in powerbuilder 12 to fetch some records . Here this is my query.
I'm trying to fetch some records from the trninvhdr table which are not in the second table.
SELECT
INV_DATE,
INV_NO,
INV_TYPE,
CUR_CODE,
EXCH_RATE,
usd_rate,
CR_TERM,
DUE_DATE,
bl_date,
TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE ='NFL1' AND
CUST_CODE = 'NLML' AND
INV_TYPE ='F' AND
INV_DATE <= '2016-03-25' AND
NOT EXISTS
(SELECT * FROM trninvoiceavailability WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
Here how I use it in the program.
DECLARE lc_retrieve CURSOR FOR
SELECT
trninvhdr.INV_DATE,
trninvhdr.INV_NO,
trninvhdr.INV_TYPE,
trninvhdr.CUR_CODE,
trninvhdr.EXCH_RATE,
trninvhdr.usd_rate,
trninvhdr.CR_TERM,
trninvhdr.DUE_DATE,
trninvhdr.bl_date,
trninvhdr.TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE = :as_comp_code AND
CUST_CODE = :as_cust_code AND
INV_TYPE ='F' AND
INV_DATE <= :as_inv_date )AND
NOT EXISTS (SELECT * FROM trninvoiceavailability
WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND
trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
open lc_retrieve ;
The query works fine in the mysql server but in the progra it gives me the following error .
Database c0038 SQLSTATE = 3700 MySQL ODBC 5.2 a Driver mysql id 5.5.25 You have an error in your syntax. check the manual that corresponds to your mysql version for the right syntax to use near NOT EXISTS (SELECT * FROM trninvoiceavailability.... at line 1.
What is the correct Syntax that I should use to work this query.
I can see a bracket in this code... where did it come from and where is it's friend?
INV_DATE <= :as_inv_date )AND
You need to remove ) from your query as per below-
INV_DATE <= :as_inv_date )AND
sholuld be INV_DATE <= :as_inv_date AND

how to fix sql query error

I have this request
DELETE FROM T1 WHERE PERMISSION_ID = 'x' AND
0<(SELECT * FROM T2 WHERE "SUBSTR"(PID,1,1)=1) OR '1'='1'
but got
The SQL statement " DELETE FROM T1 WHERE PERMISSION_ID = 'x'
AND 0<(SELECT * FROM T2 WHERE SUBSTR(PID,1,1)=1) OR '1'='1'"
contains the syntax error[s]: - 1:101 - SQL syntax error: the token
"(" was not expected here
Please help, how I can fix it?
UPDATE
remove "SUBSTR" to SUBSTR, but get The SQL statement
DELETE FROM T1
WHERE PERMISSION_ID = 'x'
AND 0= (SELECT * FROM T2 WHERE SUBSTR(PID,1,1)=1) OR '1'='1'
contains the syntax error[s]: - 1:99 - SQL syntax error: the token "(" was not expected here
UPDATE 2
I change to
SELECT count(*) FROM T2 WHERE SUBSTRING("asdqweasd",1,1))
and get
- SQL syntax error: the token "," was not expected here
- expecting "from", found ','
You do not need Double quotes around SUBSTR. Also you can not use SELECT * . Use count(*)
DELETE FROM T1 WHERE PERMISSION_ID = 'x' AND
0<(SELECT count(*) FROM T2 WHERE SUBSTR(PID,1,1)=1) OR '1'='1'

embedded mysql query inside unix_timestamp

Why is this not working
...
) as Data
WHERE UNIX_TIMESTAMP(Data.DateTime) <= UNIX_TIMESTAMP(SELECT DateTime from mytable WHERE ID = $inputID)
It seems to work if I don't have this embedded sql statement, but the sql statement works on its own also so I'm not sure why the combination is causing a failure.
It's telling me
check to your MySQL server version for the right syntax to use near
'SELECT DateTime from mytable WHERE ID = 1008)' at line 1
Try surrounding the SQL with additional parens ():
WHERE UNIX_TIMESTAMP(Data.DateTime) <= UNIX_TIMESTAMP((SELECT DateTime from mytable WHERE ID = $inputID))
Use this:
...
) as Data
WHERE UNIX_TIMESTAMP(Data.DateTime) <= (SELECT UNIX_TIMESTAMP(DateTime) from mytable WHERE ID = $inputID)
If DateTime has type DateTime than you can also use:
...
) as Data
WHERE Data.DateTime <= (SELECT DateTime from mytable WHERE ID = $inputID)