This is my script from Oracle but I have problems running it on MySQL.
SELECT s.branch_no, SUM(s.staff_salary)
from staff AS s
HAVING SUM(staff_salary) = (SELECT MAX(SUM(S2.staff_salary))
FROM staff AS S2
GROUP BY S2.BRANCH_NO)
GROUP BY s.branch_no;
The error that I get when I run it on MySQL:
SELECT s.branch_no, SUM(s.staff_salary)
from staff AS s
HAVING SUM(staff_salary) = (SELECT MAX(SUM(S2.staff_salary))
FROM staff AS S2
GROUP BY S2.branch_no)
GROUP BY s.branch_no
LIMIT 0, 50
MySQL said:
#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 'GROUP BY s.branch_no LIMIT 0, 50' at line 7
Try seperating the query into small subqueries.
In your case I would try:
SELECT *
FROM (
SELECT SUM(staff_salary) sm , branch_no
FROM staff
GROUP BY BRANCH_NO
) s
ORDER BY sm DESC LIMIT 1
Related
I tried ORDER BY and GROUP BY together on statement, and ORDER by is not working; this is the code: SELECT ter_code, ter_name, tgp_longitude, tgp_latitude FROM tblterminalgps, tblterminal WHERE ter_status != ? AND tgp_teruid = ter_uid GROUP BY ter_uid ORDER BY tgp_datecreated DESC
On second try I tried this one: SELECT t2.ter_code, t2.ter_name, t1.tgp_longitude, t1.tgp_latitude FROM tblterminalgps as t1 INNER JOIN tblterminal as t2 ON t1.tgp_teruid = t2.ter_uid
(SELECT * FROM tblterminalgps GROUP BY t1.tgp_teruid DESC)
ORDER BY tgp_datecreated DESC
and I am getting this kind of error: 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 * FROM tblterminalgps GROUP BY t1.tgp_teruid DESC)
ORDER BY tgp_datecrea' at line 2
I have a SQL query like this:
SELECT E.snum
FROM Enrolled E
GROUP BY E.snum
HAVING COUNT (*) >= ALL (SELECT COUNT(*)
FROM enrolled E2
GROUP BY E2.snum)
But it generates an 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 ') >= ALL (SELECT COUNT() FROM enrolled E2 GROUP BY E2.snum)
LIMIT 0, 30' at line 1
What does the error mean? Why it doesn't allow me to use ALL operator?
Try this
SELECT E.snum, COUNT(E.snum)
FROM Enrolled E
GROUP BY E.snum
HAVING COUNT (E.snum) >= ALL (SELECT COUNT(*)
FROM enrolled E2
GROUP BY E2.snum)
Please have a look at the below query.
SELECT sub_words.idwords, words_inc.idArticle
(
SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS excl_words, COUNT(sub1.idwords) AS older_words_cnt
FROM words_learned sub0
LEFT OUTER JOIN words_learned sub1
ON sub0.userId = sub1.userId
AND sub0.order < sub1.order
WHERE sub0.userId = 1
GROUP BY sub0.idwords
) sub_words
INNER JOIN words words_inc
ON sub_words.idwords = words_inc.idwords
LEFT OUTER JOIN words words_exc
ON words_inc.idArticle = words_exc.idArticle
AND FIND_IN_SET(words_exc.idwords, sub_words.excl_words)
WHERE words_exc.idwords IS NULL
ORDER BY older_words_cnt
LIMIT 100
This gives the 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 'SELECT sub0.idwords, SUBSTRING_INDEX(GROUP_CONCAT(sub1.idwords), ',', 10) AS exc' at line 3
I checked the sub query individually and there was no error in the sub query! what is going on here?
You missed the from keyword
SELECT sub_words.idwords, words_inc.idArticle
FROM
( ...
I am getting a syntax error can anybody tell me why?
SELECT c.clientid, c.clientname, c.billingdate,
(SELECT TOP 1 previousbalance FROM invoice i
WHERE i.client = c.clientid ORDER BY i.invoiceid DESC) AS remaining
FROM client c
ORDER BY clientname
What the secondary select is doing is getting the latest record for that clientid in the invoice table.
The program - HediSQl
SQL
And here is the error:
SQL 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 '1 previousbalance FROM invoice i WHERE i.client = c.clientid ORDER BY i.invoicei' at line 1 */
Just guessing but it might indicate that you should replace TOP 1 with LIMIT 1 or WHERE ROWNUM < 2 LIMIT 1. What kind of DB are you using?
You just need to use LIMIT instead TOP like this:
SELECT c.clientid, c.clientname, c.billingdate,
(SELECT previousbalance FROM invoice i
WHERE i.client = c.clientid ORDER BY i.invoiceid DESC LIMIT 1) AS remaining
FROM client c
ORDER BY clientname
See this SQLFiddle
The syntax you have will work in SQL Server. Your error message is from MySQL.
Try this:
SELECT c.clientid,
c.clientname,
c.billingdate,
(
SELECT previousbalance
FROM invoice i
WHERE i.client = c.clientid
ORDER BY i.invoiceid DESC
LIMIT 1
) AS remaining
FROM client c
ORDER BY clientname
I have the following query:
SELECT * FROM
(SELECT t1.`id`, t1.`vehicle`, lp1.`veh_no` AS `lp_vehicle`,
t1.`last_date`, t1.`due_date`, t1.`driver`, lp4.`employ_name` AS `lp_driver`
FROM `inspection` AS t1 LEFT OUTER JOIN `vehicle` AS lp1
ON (t1.`vehicle` = lp1.`id`)
LEFT OUTER JOIN `employee_driver` AS lp4
ON (t1.`driver` = lp4.`id`)) subq,
WHERE MONTH(t1.`due_date`) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH))
ORDER by vehicle asc;
It processes through fine until I get to the WHERE clause.
This is what I get on the above:
#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
'WHERE MONTH(t1.`due_date`) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH))
ORDER b'
at line 1
Can someone please point out what I am doing wrong? I'm running MySQL 5.1.48
you have an extra comma after subq