0
特定の半径の特定の座標の中央値と平均を計算します。MySQL:特定の半径(緯度/経度)の中央値を計算するには
重要な属性は以下のとおりです。 - 緯度 - 経度 - 価格
は、平均値を計算するには、SQLコマンドは次のとおりです。
SELECT avg(price) as average
FROM (SELECT r.*,
(6371 * acos(cos(radians(37.3541079)) * cos(radians(ANY_VALUE(`latitude`))) * cos(radians(ANY_VALUE(`longitude`)) - radians(-121.9552356)) + sin(radians(37.3541079)) * sin(radians(ANY_VALUE(`latitude`))))) AS distance
FROM `Rental` r
) r
WHERE distance <= 20;
私の質問は、私は価格のための中央値を計算することができる方法であります指定された座標と半径で返します。 MySQLにはmedian()関数はありません。
EDIT : は今、私はSimple way to calculate median with MySQLそれはすべての200'000レコードのために働く
SELECT AVG(middle_values) AS 'median' FROM (
SELECT t1.price AS 'middle_values' FROM
(
SELECT @row:[email protected]+1 as `row`, x.price
FROM rental AS x, (SELECT @row:=0) AS r
WHERE 1
-- put some where clause here
ORDER BY x.price
) AS t1,
(
SELECT COUNT(*) as 'count'
FROM rental x
WHERE 1
-- put same where clause here
) AS t2
-- the following condition will return 1 record for odd number sets, or 2 records for even number sets.
WHERE t1.row >= t2.count/2 and t1.row <= ((t2.count/2) +1)) AS t3;
からのコードを試してみましたが、私が追加されますWHERE distance <= 20
は、MySQLである - 要求が過負荷します。
SELECT AVG(middle_values) AS 'median' FROM (
SELECT t1.price AS 'middle_values' FROM
(
SELECT @row:[email protected]+1 as `row`, x.price
FROM rental AS x, (SELECT @row:=0) AS r, (SELECT a.*,
(6371 * acos(cos(radians(37.3541079)) * cos(radians(ANY_VALUE(`latitude`))) * cos(radians(ANY_VALUE(`longitude`)) - radians(-121.9552356)) + sin(radians(37.3541079)) * sin(radians(ANY_VALUE(`latitude`))))) AS distance
FROM `Rental` a
) a
WHERE distance <= 20
-- put some where clause here
ORDER BY x.price
) AS t1,
(
SELECT COUNT(*) as 'count'
FROM rental x, (SELECT a.*,
(6371 * acos(cos(radians(37.3541079)) * cos(radians(ANY_VALUE(`latitude`))) * cos(radians(ANY_VALUE(`longitude`)) - radians(-121.9552356)) + sin(radians(37.3541079)) * sin(radians(ANY_VALUE(`latitude`))))) AS distance
FROM `Rental` a
) a
WHERE distance <= 20
-- put same where clause here
) AS t2
-- the following condition will return 1 record for odd number sets, or 2 records for even number sets.
WHERE t1.row >= t2.count/2 and t1.row <= ((t2.count/2) +1)) AS t3;
どこかに間違いがありますか?
質問は? – e4c5
どのようにして、指定された座標と半径で価格の中央値を計算できますか? MySQLにはmedian()関数はありません。 – Severin
[MySQLでのメジアンを計算する簡単な方法]の複製の可能性があります。(http://stackoverflow.com/questions/1291152/simple-way-to-calculate-median-with-mysql) – e4c5