getChooseElectricityPrice.sql 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 (
  23. SELECT
  24. -- p.name pastureName,
  25. -- p.sort,
  26. '电费(万元)' ftype,
  27. ROUND(qq.monthBudget,2) monthBudget, #预算
  28. ROUND(qq.monthLimit,2) monthLimit, #内控
  29. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  30. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  31. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  32. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  33. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  34. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  35. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  36. IFNULL(ROUND(qq.sumPrice/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  37. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  38. qq.pastureId
  39. FROM
  40. (SELECT
  41. tem.pastureId,
  42. IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  43. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='电费' AND pastureId = tem.pastureId),0)monthBudget,
  44. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  45. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='电费' AND pastureId = tem.pastureId ),0)monthLimit,
  46. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  47. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='电费' AND pastureId = tem.pastureId ),0)cowSumBudgetMonth,
  48. 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,
  49. tem.startTime,
  50. tem.endTime,
  51. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  52. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  53. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  54. 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,
  55. tem.sumPrice
  56. FROM(SELECT
  57. m.pastureId,
  58. ROUND(IFNULL(SUM(e.elecConsumption * e.`price`),0),2) sumPrice,
  59. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  60. 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
  61. FROM
  62. electricity e
  63. INNER JOIN `measure` m
  64. ON m.id = e.measureId
  65. WHERE m.meterType = '电表' AND m.useType='2级表' AND e.date >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  66. AND e.date < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  67. AND (m.pastureId = ? OR ? in ('18',''))
  68. AND (m.departmentId = ? or ? = '')
  69. GROUP BY m.pastureId ) tem
  70. ) qq
  71. )t
  72. left join
  73. pasture pp on pp.id = t.pastureId
  74. where pp.id <>18
  75. group by pp.center,pp.region,pp.name with rollup