So I want to get the median of a numeric column using mySQL.
Here is my code:
SELECT LAT_N FROM STATION
ORDER BY LAT_N
LIMIT SELECT FLOOR(COUNT(*)/2) FROM STATION,1
Where LAT_N is the column name that I want to check and STATION is the table name. I got the following error, But I could not understand which syntax is wrong.
ERROR 1064 (42000) at line 1: 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 FLOOR(COUNT(*)/2) FROM STATION),1' at line 3
Could you help me to find the syntax error? If possible I would like to calculate the median using the same way.
You can use window functions instead:
SELECT LAT_N
FROM (SELECT S.*,
COUNT(*) OVER () AS CNT,
ROW_NUMBER() OVER (ORDER BY LAT_N) AS SEQNUM
FROM STATION S
) S
WHERE SEQNUM = FLOOR(CNT / 2);
you can use prepared statements, as Limit only accepts Numbers
SELECT FLOOR(COUNT(*)/2) INTO #a FROM STATION;
SET #s = 'SELECT LAT_N FROM STATION
ORDER BY LAT_N
LIMIT ?,1';
PREPARE stmt1 FROM #s;
EXECUTE stmt1 USING #a;
DEALLOCATE PREPARE stmt1;
Related
I want to use a select statement to control the limit of another select query, I cant get it to work, what I have below. The Select in the braces returns an INT.
Im newer to MySQL so im not sure what I should use instead to get this to work.
SELECT *
FROM `tbl_prod`
WHERE prod_id = 32
ORDER BY prod_level ASC , prod_date
LIMIT
(SELECT max_count
FROM Prod_subscription
WHERE prod_id = 32)
You can't write subquery in LIMIT, but you can use dynamic SQL to make your expected result.
SET #num = (
SELECT max_count
FROM Prod_subscription
WHERE prod_id = 32);
PREPARE STMT FROM 'SELECT *
FROM `tbl_prod`
WHERE prod_id = 32
ORDER BY prod_level ASC , prod_date
LIMIT ?';
EXECUTE STMT USING #num;
While running the following query
SELECT *
FROM
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY), CITY) AS T1
LIMIT 1
UNION
SELECT *
FROM
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC, CITY) AS T2
LIMIT 1;
I get
ERROR 1064 (42000) at line 4: 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 'UNION
I can't understand what is the error here
Quoting the docs,
To apply ORDER BY or LIMIT to an individual SELECT, place the clause inside the parentheses that enclose the SELECT:
(SELECT *
FROM
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY), CITY) AS T1
LIMIT 1)
UNION
(SELECT *
FROM
(SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC, CITY) AS T2
LIMIT 1)
It could be that you are using ORDER BY in both parts of union (SQL Server does not allow this). Try removing and / or ordering the final result set
I have a table that looks like this:
ID Damage
1 10
2 7
3 153587
4 1
...1M more rows
I have another table that has a column that represents the percentile in the amount of rows I need to grab, so if its top 10 percentile the column value will be 100000 I want to grab based on damage.
Is there a way instead of saying LIMIT 100000, since the percentile changes to replace 100000 with essentially a variable or the column value?
Second table:
Days Percentile_Affected Damage_Sum
14 87000
30 161000
90 371000
...
If the ids had no gaps, you could just use the id. Instead, you can add a counting variable and use that:
select t.*
from (select t.*, (#rn := #rn + 1) as rn
from (select t.*
from t
order by id
) t cross join
(select #rn := 0) params
) t
where rn < (select "a column" from "another table");
The alternative is to construct the query and use dynamic SQL:
select #sql := replace('select t.* from t limit [limit]', [limit], "a column")
from "another table";
prepare stmt from #sql;
execute stmt;
Or, use a placeholder for the limit:
set #sql = 'select t.* from t limit ?';
select #limit := "a column"
from "another table";
prepare stmt from #sql;
execute stmt using #limit;
I have problem executing this proc in my hosted mysql v-5.1. I cant find the problem or error. please help me out.
DROP PROCEDURE IF EXISTS `fetchTimeLine`;;
CREATE PROCEDURE `fetchTimeLine`(IN `delim` int(10))
BEGIN
PREPARE STMT FROM
" SELECT event_id as event,date,schedule,venue,members,descr,about,
(SELECT src FROM photo WHERE event_id=event LIMIT 0,1) as photo1,
(SELECT src FROM photo WHERE event_id=event LIMIT 1,1) as photo2,
(SELECT src FROM photo WHERE event_id=event LIMIT 2,1) as photo3,
(SELECT scr_shoot FROM videos WHERE event_id=event LIMIT 0,1) as video1,
(SELECT scr_shoot FROM videos WHERE event_id=event LIMIT 1,1) as video2,
(SELECT scr_shoot FROM videos WHERE event_id=event LIMIT 2,1) as video3,
(SELECT src FROM videos WHERE event_id=event LIMIT 0,1) as vsrc1
FROM activities
ORDER BY date desc LIMIT ?,?; ";
SET #START = delim;
SET #LIMIT = 2;
EXECUTE STMT USING #START, #LIMIT;
DEALLOCATE PREPARE STMT;
END;;
error I get:
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 '' at line 4
In MySQL, date is a reserved word, and
you have it in your SELECT clause. Try
`date`
instead.
Apply the same change in your ORDER BY clause.
i am trying to implement following query,
(SELECT
MAX(final_avg_total.`Provider Name`) AS `Hospital Name`,
final_avg_total.`DRG Definition`,
final_avg_total.`Provider Id`,
SUM(final_avg_total.avg_total_payments) AS avg_payments,
SUM(final_avg_total.avg_covered_charges) AS avg_covered,
(SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments)) / SUM(final_avg_total.avg_covered_charges) AS total_average,
1 - (SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments)) / SUM(final_avg_total.avg_covered_charges) AS total_percentage
FROM final_avg_total
GROUP BY final_avg_total.`Provider Id`
ORDER BY total_average DESC LIMIT 0,5)
Union
SELECT
MAX(final_avg_total.`Provider Name`) AS `Hospital Name`,
final_avg_total.`DRG Definition`,
final_avg_total.`Provider Id`,
SUM(final_avg_total.avg_total_payments) AS avg_payments,
SUM(final_avg_total.avg_covered_charges) AS avg_covered,
(SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments)) / SUM(final_avg_total.avg_covered_charges) AS total_average,
1 - (SUM(final_avg_total.avg_covered_charges) - SUM(final_avg_total.avg_total_payments)) / SUM(final_avg_total.avg_covered_charges) AS total_percentage
FROM final_avg_total
GROUP BY final_avg_total.`total_percentage`
ORDER BY total_average DESC LIMIT 0,5
actually both queries are almost same with only Group By is Differing, but i am getting this error.
5 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 'UNION (SELECT
MAX(final_avg_total.`Provider Name`) AS `Hospital Name`,
fin'
It can be helpful to opt for a smaller problem, if possible.
See if you get the same results if you
CREATE OR REPLACE VIEW part_one AS ... ;
CREATE OR REPLACE VIEW part_two AS ... ;
and then
SELECT * FROM part_one
UNION
SELECT * FROM part_two;
Letting a database engine comprehend the work on it piece-wise has helped me in times past. Also can aid code maintenance.
If you get dupes, UNION ALL is another handy arrow in the quiver.
this is just a regular UNION with error
http://www.sqlfiddle.com/#!2/ec657/7
and this is the 'same' UNION without error
http://www.sqlfiddle.com/#!2/ec657/8
the only difference is the parenthesis in both case, please be sure to place parenthesis just after the UNION or remove it
...
ORDER BY total_average DESC LIMIT 0,5)
Union
(SELECT --ADDED PARENTHESIS
MAX(final_avg_total.`Provider Name`) AS `Hospital Name`,
...
ORDER BY total_average DESC LIMIT 0,5) --ADDED PARENTHESIS