MySQL error: expecting EOF ";" instead of ")" - mysql

I have this code:
SELECT
date_created,
serial_number,
dense_rank() OVER ( PARTITION BY serial_number ORDER BY date_created DESC) AS "rank",
FROM table_A
;
And I am getting this error:
")" is not valid at this position, expecting EOF, ";"
I don't understadn why I am getting this error, I take the syntax from this web:
https://www.geeksforgeeks.org/mysql-partition-by-clause/
I already tried taking of the comma after "rank", because as far as I know there shouldn't be commas before the FROM statement. But the same error appears.
Any idea of what's happening?
EDIT: it is popssible that I cannot use dense_rank() because my user in this database doesn't have the permission to do that? There are many things I cannot do (for example to use INTO OUTFILE). Maybe I cannot use windows and first of all it doesn't recognize dense_rank(). In addition, the keyword "OVER" doesn't appear in blue in my MySQL Workbench.

The error comes to the fact that I am using the 5.7 version and windows functions were introduced in the 8 version.

Related

Getting SQL Error: "SELECT" is not valid at this position for this server version, expecting '(' with

I have been trying out the following query in MySQL Workbench:
SELECT NAME,LEAD,OUTCOME,COUNT(*) AS NUMBER_OUTCOME
FROM OUTCOMES_BY_USER
ORDER by NAME,LEAD,OUTCOME ASC;
However I am getting this error:
"SELECT" is not valid at this position for this server version, expecting '(' with
I have tried to find out where the error is coming from by taking away parts of the query and seeing where it breaks, it seems to work when I try:
SELECT NAME
FROM OUTCOMES_BY_USER;
However when I add in another column (as shown below) I start getting the same error:
SELECT NAME, LEAD
FROM OUTCOMES_BY_USER;
I am really not sure how to get around this error, I was trying this query in sqlfiddle and it worked fine, however my sqlfiddle suddenly stopped working and the website just flat out wont build schemas for me anymore. so I tried it out on a my universities MySQL server and have been getting this error. Please help!
LEAD() is a MySQL function, hence it is a reserved word. You can add backticks around reserved table or column names to bypass this error :
SELECT NAME,`LEAD`,OUTCOME,COUNT(*) AS NUMBER_OUTCOME
FROM OUTCOMES_BY_USER
ORDER by NAME,`LEAD`,OUTCOME ASC;

Using EXCEPT operator on MySql 5.1 version

I have 5.1 MySQL version on my server. I am trying to perform this query:
SELECT File_Name
FROM Words_DB
WHERE Word_Name=" . $element . "
EXCEPT
SELECT File_Name
FROM Files_DB
WHERE Display=0
I am getting an error:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'EXCEPT SELECT File_Name FROM Files_DB WHERE Display=0' at line 4
Can someone tell me how can i perform this query in an alternative form?
Thank you, Max.
As far as I know MySQL does not support theEXCEPToperator. Try this instead:
SELECT File_Name
FROM Words_DB
WHERE Word_Name=" . $element . "
AND File_Name NOT IN (
SELECT File_Name
FROM Files_DB
WHERE Display=0
)
You could also use either a correlatedNOT EXISTSor aLEFT JOIN. As I don't use MySQL much I can't say which performs best.
I think you can find better answers on the following site:
http://www.tutorialspoint.com/sql/sql-except-clause.htm
It says you can use except query. But you can also use answer provided by JPW above that instead of using except you can use NOT IN key word which works in the same way.

Getting Error after converting mysql query to postgresql

$ Customer.send("today").count
SELECT COUNT(*) FROM "customers" WHERE (`customers`.created_at >
'2014-02-07 05:00:00.000000')
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error
at or near "."
LINE 1: ...LECT COUNT(*) FROM "customers" WHERE (`customers`.created_at > '2014-02-07 05:00:00.000000')
When I convert my MYSQL database to PG then I m getting this error how can I fix this. If I right this query in MySQL then it's working fine but in PG getting error.
Help me Please...!
Thanks In Advance
Backticks for quoting is a MySQL-ism, standard SQL and PostgreSQL use double quotes for quoting identifiers. Somewhere you have:
where('`customers`.created_at > ?', something)
but PostgreSQL wants to see:
where('"customers".created_at > ?', something)
However, since the table name is lower case and not a reserved word, you can drop the quotes and get something that is both easier to read and will work in both databases:
where('customers.created_at > ?', something)
Presumably this change needs to be made inside the send scope.

Rails & MySQL: SELECT Statement Single vs. Double Quotes

I have a CRON job which executes a SELECT statement to grab records. When the SELECT runs on my dev machine, it produces the following statement:
SELECT `users`.* FROM `users` WHERE `users`.`id` = 87 LIMIT 1
This is successful.
When the SELECT runs on my production (hosted) machine it produces the statement with double quotes:
SELECT "users".* FROM "users" WHERE "users”.”id” = 87 LIMIT 1
This is not successful and I get a MySQL 1064 error,
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.* FROM "users" WHERE "users
The code is the same on both machines, but my dev MySQL is version 5.5.33, whereas production is 5.1.67 (I don't have control over this to set/update it)
Is there a way to force single quotes or another preferred method to handle this situation?
Thanks for your time and assistance.
--EDIT--
Here are the main code snippets that are invoked via my CRON job:
/lib/tasks/reports.rake
namespace :report do
desc "Send Daily Report"
task :daily => :environment do
User.where(:report_daily => 1).find_each do |user|
ReportsMailer.send_report(user, 'daily').deliver
end
end
/app/mailers/reports_mailer.rb
def send_report(user, date_increment)
#user = user
#date_increment = date_increment
get_times(user)
mail :to => user.email, :subject=> "Report: #{#dates}"
end
--EDIT2--
So it looks like I need to use slanted single quotes (`) in order for this to work successfully. How do I force my app or MySQL to use these instead of double (") quotes?
I don't know why it does this, but I do know that if you're referencing column names in MYSQL, you need to use ``, whereas values / data should be wrapped in "", like this:
SELECT `users`.* FROM `users` WHERE `users`.`id` = "87" LIMIT 1
I learnt this the hard way back in the day when I was learning how to do simple MYSQL queries
Here's some documentation from MYSQL's site for you:
The identifier quote character is the backtick (“`”):
mysql> SELECT * FROM `select` WHERE `select`.id > 100;
Identifier quote characters can be included within an identifier if
you quote the identifier. If the character to be included within the
identifier is the same as that used to quote the identifier itself,
then you need to double the character. The following statement creates
a table named a`b that contains a column named c"d:
mysql> CREATE TABLE `a``b` (`c"d` INT);
Is there any reason you couldn't put some of your sql statement directly into your code like:
User.where("`report_daily`=1").find_each do |user|
After further inspection, and working with my hosting company, its turns out that my query is timing out on their server. Thanks to all that responded.
Since you are not using any literals, the format of the generated SQL statements should be determined by the underlying adapter. Perhaps you have a different mysql adapter installed or configured on each machine. Check the installed version. For example:
bundle show mysql
and also check the adapter configuration for your project in database.yml. For example:
adapter: mysql
A comparison of the results of these checks between each machine should tell you if you are using different adapters on the two machines.

SQL syntax in openquery - apostrophes inside query

I have the following issue, I trying to obtain data via linked server in sql server 2008 from BMC Remedy
Everything is fine with connection, but when I added WHERE
"Assigned Group" LIKE '*scri%'*, I get error in sql server because of apostrophes which I have to use because BMC Remedy demands it.
Do you know how to create correct syntax or force sql server to use quotation marks instead of apostrophes, or disable spell checking
SELECT *
FROM OPENQUERY(Remedy,
**'**
SELECT
Incident_Number
FROM
HPD_Help_Desk
WHERE
"Assigned Group" LIKE ' scri% '
**'**
)
When doing SQL queries from within Remedy, I usually create a new field and use workflow to build the SQL query.
Also the syntax of the where clause you specified isn't correct. Try this instead:
SELECT
Incident_Number
FROM
HPD_Help_Desk
WHERE
Assigned_Group LIKE 'scri%'
There maybe a white spaces that cause you a problems.
You can also try this one:
SELECT Incident_Number
FROM HPD_Help_Desk
WHERE Assigned_Group LIKE '%scri%'
Or you can try to run this one if you run sql on DB:
SELECT r.Incident_Number
FROM ARADMIN.HPD_Help_Desk as r
WHERE r.Assigned_Group LIKE '%scri%'
Because you're running OPENQUERY, maybe double apostrophes will be needed or double quotes instead of one quote (" intead of ').
Good Luck