sql實現同環比(同比,環比)

sql實現同比,環比sql

環比同比用於描述統計數據的變化狀況。微信

公式:flex

     同比增加率=(本期數-同期數)÷同期數×100%

SQL圖片示例:
spa


SQL代碼:.net


  1. select 3d

  2. t1.ptdatecode

  3. ,t1.areaorm

  4. ,t1.goods_nameblog

  5. ,t1.income圖片

  6. ,ifnull(t1.income,0)/ifnull(t2.income,0)-1 as year_on_year_income

  7. ,t1.user_count

  8. ,ifnull(t1.user_count,0)/ifnull(t2.user_count,0)-1 as year_on_year_user_count

  9. ,'同比' as mark

  10. from bdl_goods_income_public t1

  11. join bdl_goods_income_public t2

  12. on(t1.area=t2.area and t1.goods_name=t2.goods_name and t2.ptdate=DATE_SUB('2019-04-22',INTERVAL 6 day))

  13. where t1.area ='china'

  14. and t1.ptdate = '2019-04-22'

  15. union all

  16. select

  17. t1.ptdate

  18. ,t1.area

  19. ,t1.goods_name

  20. ,t1.income

  21. ,0 as year_on_year_income

  22. ,t1.user_count

  23. ,0 as year_on_year_user_count

  24. ,'覈對數據' as mark

  25. from bdl_goods_income_public t1

  26. where t1.area ='china'

  27. and ptdate=DATE_SUB('2019-04-22',INTERVAL 6 day)

  28. ;


 同比環比公式

 



本文分享自微信公衆號 - SQL數據分析(dianwu_dw)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索