Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

MySQL N पंक्तियों की चलती औसत की गणना करता है

योजना

क्वेरी

select curr.date, curr.security_id, avg(prev.close)
from history curr
inner join history prev 
on prev.`date` between date_sub(curr.`date`, interval 49 day) and curr.`date`
and curr.security_id = prev.security_id
group by 1, 2
order by 2, 1
;

आउटपुट

+---------------------------+-------------+--------------------+
|           date            | security_id |  avg(prev.close)   |
+---------------------------+-------------+--------------------+
| January, 04 2016 00:00:00 | 1           | 10.770000457763672 |
| January, 05 2016 00:00:00 | 1           | 10.800000190734863 |
| January, 06 2016 00:00:00 | 1           | 10.673333485921225 |
| January, 07 2016 00:00:00 | 1           | 10.59250020980835  |
| January, 08 2016 00:00:00 | 1           | 10.432000160217285 |
| January, 11 2016 00:00:00 | 1           | 10.40166680018107  |
| January, 12 2016 00:00:00 | 1           | 10.344285828726631 |
| January, 13 2016 00:00:00 | 1           | 10.297500133514404 |
| January, 14 2016 00:00:00 | 1           | 10.2877779006958   |
| January, 04 2016 00:00:00 | 2           | 56.15999984741211  |
| January, 05 2016 00:00:00 | 2           | 56.18499946594238  |
| ..                        | ..          | ..                 |
+---------------------------+-------------+--------------------+

sqlfiddle

संदर्भ

पिछली 50 पंक्तियों का उपयोग करने के लिए संशोधित

select
rnk_curr.`date`, rnk_curr.security_id, avg(rnk_prev50.close)
from
(
select `date`, security_id,
@row_num := if(@lag = security_id, @row_num + 1,
               if(@lag := security_id, 1, 1)) as row_num
from history 
cross join ( select @row_num := 1, @lag := null ) params
order by security_id, `date`
) rnk_curr
inner join
(
select date, security_id, close,
@row_num := if(@lag = security_id, @row_num + 1,
               if(@lag := security_id, 1, 1)) as row_num
from history 
cross join ( select @row_num := 1, @lag := null ) params
order by security_id, `date`
) rnk_prev50
on  rnk_curr.security_id = rnk_prev50.security_id
and rnk_prev50.row_num between rnk_curr.row_num - 49 and rnk_curr.row_num
group by 1,2
order by 2,1
;

sqlfiddle

नोट

यदि फ़ंक्शन चर के मूल्यांकन के सही क्रम को बाध्य करना है।



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. ansi_nulls . के बारे में कुछ

  2. कैसे जांचें कि सक्षम/अक्षम कुंजियां काम करती हैं या नहीं?

  3. मैं हाइबरनेट + c3p0 + MySql को बड़ी संख्या में स्लीपिंग कनेक्शन बनाने से कैसे रोक सकता हूँ?

  4. SQL कथन अनदेखा कर रहा है जहां पैरामीटर

  5. MySQL IF शर्तों के साथ जुड़ें