MySQL statistics yoy SQL

Source: Internet
Author: User

General Idea:

MySQL does not have a convenient statistical function like Oracle, can only rely on their own hard computing: through the Time field directly increase the year, the month, and then through the left join associated Time field to calculate the chain, year-over formula can be

Original table structure:



Find sql:

[SQL]View PlainCopyPrint?
  1. --by year   
  2. SELECT t5.*, Case when t5.last_energy_year is NULL OR t5.last_energy_year =0 Then 0.00
  3. ELSE FORMAT (((t5.energy_year-t5.last_energy_year)/t5.last_energy_year) *100,2)
  4. END YoY --YoY   
  5. from  (   
  6. SELECT T3.*,t4.last_energy_year
  7. from    
  8. (
  9. SELECT date_format (CONCAT (t1. ' Tyear ', '-', t1. ' Tmonth ',' -01 '), '%y-%m-%d ' as Yearmonth,t1.tyear,SUM(t1.energy_month) Energy_year, T1.linename
  10. from (SELECT
  11. t.*,COUNT(DISTINCT T. ' Tyear ', T. ' Tmonth ', T. ' Linename ') C_cot
  12. from ' ksh_tgyjy_llxgyjydlxx ' t
  13. GROUP by T. ' Linename ', T. ' Tyear ', T. ' Tmonth ', T. ' Linename ') T1
  14. GROUP by t1.tyear,t1. ' Linename '
  15. ) T3
  16. Left JOIN
  17. (
  18. SELECT Date_add (Date_format (CONCAT (t1. ' Tyear ', '-', t1. ' Tmonth ',' -01 '), '%y-%m-%d ' ), INTERVAL 1 year) as Lastyearmonth,t1.tyear,SUM (t1.energy_month) last_energy_year,t1.linename
  19. from (SELECT t.*,COUNT(DISTINCT T. ' Tyear ', T. ') Tmonth ', T. ' Linename ') C_cot
  20. from ' ksh_tgyjy_llxgyjydlxx ' t
  21. GROUP by T. ' Linename ', T. ' Tyear ', T. ' Tmonth ', T. ' Linename ') T1
  22. GROUP by t1.tyear,t1. ' Linename '
  23. ) T4
  24. on T3. Yearmonth = T4.lastyearmonth
  25. and t3.linename = T4.linename
  26. ) T5
--year-over-years select t5.*, case when T5.last_energy_year was NULL OR t5.last_energy_year =0 then 0.00      &nb Sp         ELSE FORMAT (((t5.energy_year-t5.last_energy_year)/t5.last_energy_year) *100,2)         end yoy  --yoy from (   select t3.*,t4.last_energy_year     from     (       SELECT date_format (CONCAT T1 ' tyear ', '-', t1. ' Tmonth ', '-01 ') ), '%y-%m-%d ') as Yearmonth,t1.tyear,sum (t1.energy_month) energy_year,t1.linename         from (SELECT         t.*,count (DISTINCT t. ' Tyear ', T. ' Tmonth ', T. ' Linename ') c_cot         from ' ksh_tgyjy_llxgyjydlxx ' t         group by T. ' Linename ', T. ' Tyear ', T. ' Tmonth ', T. ' Linename ') t1        group by T1.tyear,t1. ' Linename '       t3       left JOIN     (      select date_add (date_format (CONCAT T1 ' tyear ', '-', t1. ' Tmonth ', ' -01 '), '%y- %m-%d '), INTERVAL 1 year) as Lastyearmonth,t1.tyear,sum (t1.energy_month) last_energy_year,t1.linename         from (SELECT t.*,count (DISTINCT t. ' Tyear ', T. ' Tmonth ', T. ' Linename ') c_cot         from ' ksh_tgyjy_llxgyjydlxx ' t         group by T. ' Linename ', T. ' Tyear ' t. ' Tmonth ', T. ' Linename ') t1        group by t1.tyear,t1. ' Linename '    ) t4& nbsp   on T3. Yearmonth = t4.lastyearmonth    and t3.linename = t4.linename) T5

Query Result:


Find the month of SQL:

[SQL]View PlainCopyPrint?
  1. --month-on-year   
  2. SELECT Date_format (T5. Yearmonth,'%y-%m ') yearmonth,t5. ' Linename ', T5. ' Energy_month ',
  3. Case  when t5.lat_energy_month is NULL OR T5.lat_energy_month=0 then 0.00
  4. ELSE FORMAT (((t5.energy_month-t5.lat_energy_month)/t5.lat_energy_month) *100,2)
  5. END YoY
  6. from  (   
  7. SELECT T3.*,t4.energy_month Lat_energy_month
  8. from    
  9. (
  10. SELECT date_format (CONCAT (t1. ' Tyear ', '-', t1. ' Tmonth ',' -01 '), '%y-%m-%d ' ) as yearmonth,t1.*
  11. from (SELECT
  12. t.*,COUNT(DISTINCT T. ' Tyear ', T. ' Tmonth ', T. ' Linename ') C_cot
  13. from ' ksh_tgyjy_llxgyjydlxx ' t
  14. GROUP by T. ' Linename ', T. ' Tyear ', T. ' Tmonth ', T. ' Linename ') T1
  15. ) T3
  16. Left JOIN
  17. (
  18. SELECT Date_add (Date_format (CONCAT (t1. ' Tyear ', '-', t1. ' Tmonth ',' -01 '), '%y-%m-%d '), INTERVAL 1 year) as lastyearmonth,t1 .*   
  19. from (SELECT t.*,COUNT(DISTINCT T. ' Tyear ', T. ' Tmonth ', T. ') Linename ') C_cot
  20. from ' ksh_tgyjy_llxgyjydlxx ' t
  21. GROUP by T. ' Linename ', T. ' Tyear ', T. ' Tmonth ', T. ' Linename ') T1
  22. ) T4
  23. on T3. Yearmonth = T4.lastyearmonth
  24. and t3.linename = T4.linename
  25. ) T5
--month-on-year select Date_format (T5. Yearmonth, '%y-%m ') yearmonth,t5. ' Linename ', T5. ' Energy_month ',          case when T5.lat _energy_month is NULL OR t5.lat_energy_month=0 then 0.00                  else FORMAT ((t5.energy_month-t5.lat_energy_month)/t5.lat_energy_month *100,2)         end Yoyfrom (   select t3.*,t4.energy_month lat_energy_month      From     (      select date_format (CONCAT (t1. ' Tyear ', '-', t1. ' Tmonth ', ' -01 '), '%y-%m-%d ' As yearmonth,t1.*        from (SELECT           T.*,count ( DISTINCT T. ' Tyear ', T. ' Tmonth ', T. ' Linename ') c_cot           from ' KSH_TGYJY_LLXGYJYDLXX ' T           GROUP by T. ' Linename ', T. ' Tyear ', T. ' Tmonth ', T. ' Linename ') t1      ) T3        Left JOIN     (      select date_add (date_format CONCAT (T1 ' tyear ', '-', T1. ' Tmonth ', ' -01 '), '%y-%m-%d '), INTERVAL 1 year) as lastyearmonth,t1.*         from (SELECT t.*, COUNT (DISTINCT t. ' Tyear ', T. ' Tmonth ', T. ' Linename ') c_cot         from ' Ksh_tgyjy_ LLXGYJYDLXX ' t         group by T. ' Linename ', T. ' Tyear ', T. ' Tmonth ', T. ' Linename ') t1    ) t4    on T3. Yearmonth = t4.lastyearmonth    and t3.linename = t4.linename) T5

Query Result:


MySQL statistics yoy SQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.