I need to return average for each of 12 columns I have in a table in DB. MySQL allows one to get average for one column only. The following query (for one column) works:
SELECT station_id, AVG(jan) AS avg_jan
FROM `climate_data`
WHERE element_name = "Temp_mean_mly" AND jan <> -999999
GROUP BY station_id
and the following (for multiple columns) does not (I get syntax error):
SELECT station_id, AVG(jan) AS avg_jan, AVG(feb) AS avg_feb, ... ,
AVG(dec) AS avg_dec
FROM `climate_data`
WHERE element_name = "Temp_mean_mly"
AND jan <> -999999
AND feb <> -999999
AND ...
AND dec <> -999999
GROUP BY station_id
Do I have to use 12 sub-queries to achieve the result I need?
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 'dec)
dec is a reserved MySQL keyword; changing it to `dec` in your query would probably fix that error for you :).
Edit: note that you're also using it in the WHERE clause; it might work there (as it's unlogical for MySQL to find a keyword there), but keep it in mind that you might also have to escape that one :)
Related
In my SQL table I have "country" and "we200326" columns. Column "we200326" contains only "1" or "NULL" entries.
I'm trying to get a total of all "1"s in column "we200326" and a total by country. I have written the following statement but it gives an error but I don't know what I did wrong (I'm very new at this):
SELECT country, we200326,
(SUM(we200326) OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
FROM table_name
ORDER BY CountryTotal, Country;
The error I get is this:
MySQL said: Documentation
#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 'OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
' at line 2
I have searched for similar errors and found several (each time was a simple syntax error like a space or comma or so) I tried several versions but could not resolve my problem when following those instructions. Any help would be appreciated.
Window functions are available in MySQL 8.0 only.
In earlier versions, one option is to use subqueries:
select
country,
wewe200326,
(select sum(we200326) from table_name) total,
(select sum(we200326) from table_name t1 where t1.country = t.country) country_total
from table_name t
order by country_total, country
i have a table and i have columns. I want to get AGE column- it is creating (current date - year column data)- and whole columns like *.
I tried;
SELECT (YEAR(CURRENT_DATE) - YEAR(dateOfBirth)) as age , * FROM users
but it gives 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 LIMIT 0, 25' at line 1
What should i write? Thank you!
use alias and TIMESTAMPDIFF function
SELECT u.*,
TIMESTAMPDIFF(YEAR, dateOfBirth, CURRENT_DATE) as age
FROM users u
'Use of an unqualified * with other items in the select list may produce a parse error. To avoid this problem, use a qualified tbl_name.* reference' - https://dev.mysql.com/doc/refman/8.0/en/select.html
This is ok
SELECT (YEAR(CURRENT_DATE) - YEAR(dateOfBirth)) as age, users.* FROM users
I am attempting to execute the following statement...
SELECT
SUM(CASE WHEN CLG ='A*' THEN 1 END) as A*
From Grades
However, I receive the following 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 '*,"
I realise of course that * is used in select statements to select all the rows of a table. However in my case, I want to find the number of records that contain the value of A*. Would anyone be able to point out what I can do to solve this error without having to remove the A* values from my table?
You need to use backticks:
SELECT SUM(CASE WHEN CLG ='A*' THEN 1 END) as `A*`
From Grades;
Actually you could skip CASE:
SELECT SUM(CLG ='A*') as `A*`
From Grades
Your problem is the column alias. Don't use inappropriate characters for column names. Do something like this:
SELECT SUM( CLG = 'A*' ) as A_star
From Grades;
Having to deal with identifiers that use unusual characters is just a pain -- making queries hard to write and to read.
I am executing this query in MySql:
SELECT amount
FROM Prices
WHERE (item_id = 1246 AND
('2016-12-26' BETWEEN (effective_date AND COALESCE(end_date, NOW()))))
But for some reason I get a syntax error that I don't see where it is.
the error is:
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 ')) AND(item_id = 1419 AND ('2017-01-14' BETWEEN (effective_date AND COALESCE(end' at line 1
the Price is like this:
Prices
id
item_id
effective_date
end_date
I don't think there should be parentheses between BETWEEN and the first term of that expression. Something like this should work:
SELECT amount
FROM Prices
WHERE item_id = 1246 AND
'2016-12-26' BETWEEN effective_date AND COALESCE(end_date, NOW())
This question is a typo, but maybe this answer would be useful to anyone who wants to know the proper way to use BETWEEN.
The MySQL documentation for BETWEEN doesn't explicitly mention anything about parentheses, but it seems to be implying this based on the examples given.
Based on testing this locally, parentheses around each of the two terms in the BETWEEN expression are OK, e.g.
WHERE '2016-12-26' BETWEEN (effective_date) AND (COALESCE(end_date, NOW()))
However, putting parenthesis around the entire clause generates an error, which is what you were doing:
WHERE '2016-12-26' BETWEEN (effective_date AND COALESCE(end_date, NOW()))
I'm used to running on an Oracle database, so I'm not really quite sure how to trouble shoot this problem. I've narrowed down a simple example of my query to the following:
SELECT 0 as gm_rowID,
'-ALL Grantmakers-' as grantmakerName
FROM dual
GROUP BY 2
phpMyAdmin runs the SQL with the following 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 'ORDER BY 2 LIMIT 0, 30' at line 1
Oracle can run this query just fine. MySQL can run the query without the GROUP BY clause. Any ideas?
--Here is the entire query:
SELECT
p.grantmaker_rowid as gm_rowID,
gm.grantmaker_companyName as grantmakerName
FROM grantmaker_info gm, proposal_submission p
WHERE 0=0
AND p.grantmaker_rowid = gm.grantmaker_rowid
UNION
SELECT
0 as gm_rowID,
'-ALL Grantmakers-' as grantmakerName
FROM dual
ORDER BY 2
GROUP BY 2
LIMIT 0 , 30
Columns selected for output can be referred to in ORDER BY and GROUP BY clauses using column
names, column aliases, or column positions. Column positions are
integers and begin with 1
From: http://dev.mysql.com/doc/refman/5.0/en/select.html
Unless you only have 1 column in that table, it should run fine. My suggestion however would be to reference the column name (or alias) of whatever you're trying to GROUP BY.
edit: My only other suggestion is to include the SHOW CREATE TABLE output for that table.
edit2: Ok I see you've updated your question. Why not instead of ORDER BY 2, you ORDER BY grantmakerName (if that's the column you want to order by?)