getChooseMonthiolPrice.sql 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. select
  2. round(sum(t.monthBudget),2)monthBudget,
  3. round(sum(t.monthLimit),2)monthLimit,
  4. round(sum(t.sumPrice),2)sumPrice,
  5. round(sum(t.qmonthBudget),2)qmonthBudget ,
  6. round(sum(t.asave),2)asave,
  7. round(avg(t.oneCowBudget),2)oneCowBudget,
  8. round(avg(t.oneCowLimit),2)oneCowLimit,
  9. round(avg(t.oneCowQmonthBudget),2)oneCowQmonthBudget,
  10. round(avg(t.oneCowSumPrice),2)oneCowSumPrice,
  11. round(avg(t.oneCowAsave),2)oneCowAsave,
  12. t.ftype ftype,
  13. t.realReason realReason,
  14. t.pastureId pastureId,
  15. case
  16. when pp.name is null then
  17. case when pp.center is null then '集团'
  18. else if(pp.region is null,pp.center,pp.region)
  19. end
  20. else pp.name
  21. end pastureName
  22. from (SELECT
  23. '燃动费(万元)' ftype,
  24. ROUND(qq.monthBudget,2) monthBudget, #预算
  25. ROUND(qq.monthLimit,2) monthLimit, #内控
  26. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  27. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  28. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  29. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  30. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  31. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  32. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  33. IFNULL(ROUND(qq.sumPrice/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  34. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  35. 18 pastureId
  36. FROM
  37. (SELECT IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  38. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='燃动费'),0)monthBudget,
  39. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  40. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='燃动费' ),0)monthLimit,
  41. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  42. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='燃动费' ),0) cowSumBudgetMonth,
  43. IFNULL((SELECT sum(fs.`cowNum`) FROM feed_sum fs INNER JOIN pasture p ON p.id = fs.pastureId WHERE fs.`feedDay` >= tem.startTime and fs.`feedDay` <= tem.endTime),0) cowSum,
  44. tem.startTime,
  45. tem.endTime,
  46. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  47. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  48. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  49. if (DATEDIFF(tem.endTime,CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))=0,DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1,DATEDIFF(tem.endTime,CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))) realDay,
  50. tem.sumPrice
  51. FROM(SELECT
  52. ROUND(IFNULL(SUM(d.oilAmount * d.`price`),0),2) sumPrice,
  53. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  54. IF(RIGHT(?,2)='00',CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01'),DATE_ADD(?,INTERVAL 1 DAY)) endTime
  55. FROM
  56. `diesel` d
  57. WHERE d.selTime >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  58. AND d.selTime < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  59. ) tem) qq
  60. UNION ALL
  61. SELECT
  62. '燃动费(万元)' ftype,
  63. ROUND(qq.monthBudget,2) monthBudget, #预算
  64. ROUND(qq.monthLimit,2) monthLimit, #内控
  65. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  66. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  67. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  68. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  69. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  70. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  71. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  72. IFNULL(ROUND(qq.sumPrice/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  73. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  74. qq.pastureId
  75. FROM
  76. (SELECT
  77. tem.pastureId,
  78. IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  79. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='燃动费' AND pastureId = tem.pastureId),0)monthBudget,
  80. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  81. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='燃动费' AND pastureId = tem.pastureId ),0)monthLimit,
  82. IFNULL((SELECT sum(fs.`cowNum`) FROM feed_sum fs INNER JOIN pasture p ON p.id = fs.pastureId WHERE p.id= tem.pastureId and fs.`feedDay` >= tem.startTime and fs.`feedDay` <= tem.endTime),0) cowSum,
  83. tem.startTime,
  84. tem.endTime,
  85. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  86. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  87. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  88. if (DATEDIFF(tem.endTime,CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))=0,DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1,DATEDIFF(tem.endTime,CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))) realDay,
  89. tem.sumPrice,
  90. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE budgetMonth >= tem.startTime AND budgetMonth < tem.endTime AND feeType='燃动费' AND pastureId = tem.pastureId ),0)cowSumBudgetMonth
  91. FROM(SELECT
  92. d.pastureId,
  93. ROUND(IFNULL(SUM(d.oilAmount * d.`price`),0),2) sumPrice,
  94. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  95. IF(RIGHT(?,2)='00',CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01'),DATE_ADD(?,INTERVAL 1 DAY)) endTime
  96. FROM
  97. `diesel` d
  98. WHERE d.selTime >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  99. AND d.selTime < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  100. AND (d.pastureId = ? OR ? in ('18',''))
  101. AND (d.deptId = ? or ? = '')
  102. GROUP BY d.pastureId ) tem) qq
  103. )t
  104. left join
  105. pasture pp on pp.id = t.pastureId
  106. where pp.id <>18
  107. group by pp.center,pp.region,pp.name with rollup