How to solve this error: SQLite Syntax error: near FROM - sqlite

I am getting the following error:
Execution finished with errors.
Result: near "FROM": syntax error
I am not able to locate any extra commas or any typo. Can you please check:
WITH session_sources_aggregated AS (
SELECT
EXTRACT(month FROM event_date) AS month,
channel_name,
COUNT(*) AS num_sessions,
SUM(cpc) AS total_cpc
FROM session_sources
GROUP BY month, channel_name
)
SELECT
month,
channel_name,
num_sessions,
total_cpc
FROM session_sources_aggregated
ORDER BY month, channel_name;

Related

MariaDB SQL syntax error with Output file Concatenation

What am I doing wrong here at line 7? Can't seem to figure this one out. Anyone willing to help? Thanks
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 '11:29:08' at line 7
Line 7
FIELDS TERMINATED BY ','
SET #sql_text =
CONCAT (
"SELECT price_id, customer, location, tier, channel_segment, product, best_before_date, brand, pack, price, tonnes, date_reported, created_at, week
INTO OUTFILE 'C:/Temp/price_data_"
, DATE_FORMAT( NOW(), '%Y%m%d')
, ".csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
FROM price_data_summary
WHERE date_reported >=", now()-interval 3 month);
PREPARE s1 FROM #sql_text;
EXECUTE s1;
DROP PREPARE s1;

How to use 'Between' operator for DATEs in Teradata?

I want to restrict the rows I retrieve by using 'between' for two dates.
The 'saledate' column I used has the following infomation
SALEDATE DATE FORMAT 'YYYY-MM-DD' NOT NULL
The code I used:
SELECT *
FROM trnsact
WHERE saledate BETWEEN '2005-01-01' AND '2005-06-30';
And then I got an error 'Error Code - 3535
Error Message - [Teradata Database] [TeraJDBC 15.10.00.09] [Error 3535][SQLState 22003]
A character string failed conversion to a numeric value.'
I also tried with DATE:
SELECT *
FROM trnsact
WHERE saledate BETWEEN DATE '2005-01-01' AND DATE '2005-06-30';
But end up with another error
Error Message - [Teradata Database] [TeraJDBC 15.10.00.09] [Error 3706] [SQLState 42000] Syntax error: Invalid DATE Literal.
Thanks for your help
You need to use DATE literals:
SELECT *
FROM trnsact
WHERE saledate BETWEEN DATE '2005-01-01' AND DATE '2005-06-31';
try below query for teradata. It's too late though.
SELECT *
FROM trnsact
WHERE saledate BETWEEN
to_date('2005-01-01','YYYY-MM-DD') AND
to_date('2005-06-30','YYYY-MM-DD') ;

Sqlite "WITH" syntax error

I have this code, with this error: "Query Error: near "WITH": syntax error Unable to execute statement" from both SqliteMan and from Node-webkit (latest).
WITH RECURSIVE FT_CTE(*) AS (
SELECT pID, cID FROM brFTNode_Children
WHERE pID = 1
UNION ALL
SELECT e.pID, e.cID FROM brFTNode_Children e
INNER JOIN FT_CTE ftCTE ON (ftCTE.cID = e.pID)
)
SELECT * FROM FT_CTE;
The Sqlite document clearly stated that it supports "WITH" statement.
Any idea?

razor sql error

I have this razor statement
sql = "SELECT * FROM CarBike" +
"Order By id OFFSET #0 ROWS FETCH NEXT #1 ROWS ;";
var result = db.Query(sql, offset, pageSize);
i am getting error
Incorrect syntax near the keyword 'By'.
Invalid usage of the option NEXT in the FETCH statement.
System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'By'.
Invalid usage of the option NEXT in the FETCH statement.
Please help me to correct this error
You need a space between CarBike and Order by
sql = "SELECT * FROM CarBike" +
" Order By id OFFSET #0 ROWS FETCH NEXT #1 ROWS ;";
NB: OFFSET/FETCH is SQL 2012+ only.
To achieve similar results in previous versions
select * from
(
select *, ROW_NUMBER() over (order by id) rn
from CarBike
) v
where rn between #0+1 and #0+#1
order by id

SQLite Syntax Error

I made a sql query, below. I am getting an exception which I have shown as well. Any help is appreciated.
QUERY:
INSERT OR REPLACE INTO CLAIMS (DATE ,TIME , ADDRESS , CITY,
STATE , POSTAL , PHFNAME ,PHLNAME ,PHEMAIL ,PHPHONE ,AGENCY ,POLICY ,VEHICLENAME,
YEAR ,MAKE ,MODEL ,PLATELICENSE ,LSTATE ,VIN ,DRIVERNAME,DRFNAME ,DRLNAME ,
DRPHONE ,DREMAIL ,DRLICENSE) VALUES("Wednesday, May 4, 2011",
"10:39:10 PM EDT", "400 Chatham","Pune", "Penn", "45223", "John",
"Richard","jsmith#newyahoo.com","+1-11111111111",
"Three Rivers Insurance","(null)", "(null)", "(null)",
"(null)", "(null)","(null)", "(null)", "(null)","(null)",
"(null)", "(null)","(null)", "(null)","(null)") WHERE DATE LIKE
'Wednesday, May 4,%' AND TIME = '10:39:10 PM EDT'
Error:
SQLiteManager: Likely SQL syntax error:
[ near "WHERE": syntax error ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]
I faced the same issue for sync table for insert and update at a time and finally i use.
To use the insert or replace statement you need to use the coalesce function that required primary key column to put the where clause.
COALESCE((SELECT PRIMARY_KEY_COLUMN FROM TABLE_NAME WHERE UNIQUE_COLUMN = 'VALUE'),
(SELECT MAX(PRIMARY_KEY_COLUMN)+1 FROM TABLE_NAME))
it worked for me e.g
INSERT OR REPLACE INTO TBL_ADDRESSES
(ADDRESS_ID,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS_NAME,CITY,DB_ID)
VALUES (
COALESCE((SELECT ADDRESS_ID FROM TBL_ADDRESSES WHERE DB_ID = '111'),
(SELECT MAX(ADDRESS_ID)+1 FROM TBL_ADDRESSES)),
'IT 27','Pratap DSFSDSDDSDSF','test add ','IT 27','Jaipur','111') ;
INSERT OR REPLACE doesn't support WHERE clause, see theSQLite Insert doc. You can use a WHERE clause in UPDATE statements, see the SQLite Insert doc
Can help you if you can let us know what you wanted here ?

Resources