getChooseMonthWX.sql 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. SELECT
  2. sum(tem.asave) asave ,
  3. sum(tem.monthBudget) monthBudget ,
  4. sum(tem.monthLimit) monthLimit ,
  5. sum(tem.qmonthBudget) qmonthBudget ,
  6. sum(tem.sumPrice) sumPrice ,
  7. avg(tem.oneCowAsave) oneCowAsave ,
  8. avg(tem.oneCowBudget) oneCowBudget ,
  9. avg(tem.oneCowLimit) oneCowLimit ,
  10. avg(tem.oneCowQmonthBudget) oneCowQmonthBudget ,
  11. avg(tem.oneCowSumPrice) oneCowSumPrice ,
  12. tem.ftype ftype ,
  13. tem.pastureId pastureId ,
  14. tem.realReason realReason ,
  15. tem.sort sort ,
  16. case
  17. when pp.name is null then
  18. case when pp.center is null then '集团'
  19. else if(pp.region is null,pp.center,pp.region)
  20. end
  21. else pp.name
  22. end pastureName
  23. from
  24. (SELECT
  25. '现代牧业' pastureName,
  26. 20 sort,
  27. '维修费(万元)' ftype,
  28. ROUND(sum(qq.monthBudget),2) monthBudget, #预算
  29. ROUND(sum(qq.monthLimit),2) monthLimit, #内控
  30. ROUND(sum(qq.sumPrice)/10000,2) sumPrice, #迄今实际
  31. ROUND( sum(qq.monthLimit)/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  32. ROUND( sum(qq.monthLimit)/qq.sumDay*qq.realDay-(sum(qq.sumPrice)/10000),2) asave, #实际节约
  33. ROUND(1+(( sum(qq.monthLimit)/qq.sumDay*qq.realDay-(sum(qq.sumPrice)/10000))/(sum(qq.monthLimit)/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  34. IFNULL(ROUND(sum(qq.monthBudget)/sum(qq.cowSumBudgetMonth)*10000,2),0) oneCowBudget, #单头牛预算
  35. IFNULL(ROUND(sum(qq.monthLimit)/sum(qq.cowSumBudgetMonth)*10000,2),0) oneCowLimit, #单头牛内控
  36. IFNULL(ROUND((sum(qq.monthLimit)/qq.sumDay*qq.realDay)/(sum(qq.cowSumBudgetMonth)/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  37. IFNULL(ROUND(sum(qq.sumPrice)/(sum(qq.cowSum)/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  38. IFNULL(ROUND((sum(qq.monthLimit)/qq.sumDay*qq.realDay-(sum(qq.sumPrice)))/(sum(qq.cowSum)/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  39. 18 pastureId
  40. FROM (SELECT IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  41. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='维修费' AND (SELECT iscal FROM department d WHERE d.id=month_budget.`deptId`)= 0),0)monthBudget,
  42. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  43. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='维修费' AND (SELECT iscal FROM department d WHERE d.id=month_budget.`deptId`)= 0 ),0)monthLimit,
  44. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  45. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='维修费' AND (SELECT iscal FROM department d WHERE d.id=month_budget.`deptId`)= 0 ),0)cowSumBudgetMonth,
  46. 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,
  47. tem.startTime,
  48. tem.endTime,
  49. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  50. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  51. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  52. 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,
  53. tem.sumPrice
  54. FROM(SELECT
  55. ROUND(IFNULL(SUM( pu.`sumPrice` - pu.`quitNumber` * pu.`price`),0),2) sumPrice,
  56. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  57. 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
  58. FROM
  59. partuse pu
  60. INNER JOIN bigpartuse bpu
  61. ON pu.`bigId` = bpu.`id`
  62. WHERE bpu.`useType` <> 3 AND bpu.`receiveTime` >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  63. AND bpu.`receiveTime` < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  64. AND (
  65. pu.`useTypeV` = '维修'
  66. OR pu.`useTypeV` = '保养'
  67. ) AND (SELECT iscal FROM department d WHERE d.id=bpu.`departmentId`)= 0
  68. GROUP BY LEFT(bpu.`receiveTime`,7) ) tem) qq
  69. UNION ALL
  70. SELECT
  71. p.name pastureName,
  72. p.sort,
  73. '维修费(万元)' ftype,
  74. ROUND(qq.monthBudget,2) monthBudget, #预算
  75. ROUND(qq.monthLimit,2) monthLimit, #内控
  76. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  77. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  78. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  79. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  80. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  81. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  82. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  83. IFNULL(ROUND(qq.sumPrice/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  84. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  85. qq.pastureId
  86. FROM
  87. (SELECT tem.pastureId,IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  88. AND concat(budgetMonth,'-','01') < tem.endTime AND pastureId = tem.pastureId AND feeType='维修费' AND pastureId = tem.pastureId AND (SELECT iscal FROM department d WHERE d.id=month_budget.`deptId`)= 0),0)monthBudget,
  89. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  90. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='维修费' AND pastureId = tem.pastureId AND (SELECT iscal FROM department d WHERE d.id=month_budget.`deptId`)= 0 ),0)monthLimit,
  91. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  92. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='维修费' AND pastureId = tem.pastureId AND (SELECT iscal FROM department d WHERE d.id=month_budget.`deptId`)= 0 ),0)cowSumBudgetMonth,
  93. 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,
  94. tem.startTime,
  95. tem.endTime,
  96. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  97. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  98. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  99. 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,
  100. tem.sumPrice
  101. FROM(SELECT
  102. bpu.pastureId,
  103. ROUND(IFNULL(SUM( pu.`sumPrice` - pu.`quitNumber` * pu.`price`),0),2) sumPrice,
  104. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  105. 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
  106. FROM
  107. partuse pu
  108. INNER JOIN bigpartuse bpu
  109. ON pu.`bigId` = bpu.`id`
  110. WHERE bpu.`useType` <> 3 AND bpu.`receiveTime` >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  111. AND bpu.`receiveTime` < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  112. AND (
  113. pu.`useTypeV` = '维修'
  114. OR pu.`useTypeV` = '保养'
  115. )
  116. AND (SELECT iscal FROM department d WHERE d.id=bpu.`departmentId`)= 0
  117. AND (bpu.pastureId = ? OR ? in ('18',''))
  118. AND (bpu.departmentId = ? or ? = '')
  119. GROUP BY bpu.pastureId ) tem) qq
  120. INNER JOIN pasture p
  121. ON p.id = qq.pastureId
  122. )tem
  123. left join
  124. pasture pp on pp.id = tem.pastureId
  125. where pp.id <>18
  126. group by pp.center,pp.region,pp.name with rollup