getChooseEqMonthBudgetListSumAll.sql 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. '现代牧业' pastureName,
  25. 20 sort,
  26. #RIGHT(rr.wmonth,2) num,
  27. '设备指标(万元)'ftype,
  28. ROUND(sum(rr.monthBudget),2) monthBudget, #预算
  29. ROUND(sum(rr.monthLimit),2) monthLimit, #内控
  30. ROUND(sum(rr.sumPrice),2) sumPrice, #迄今实际
  31. ROUND( sum(rr.monthLimit)/rr.sumDay*rr.realDay,2) qmonthBudget, #迄今内控
  32. ROUND( sum(rr.monthLimit)/rr.sumDay*rr.realDay-(sum(rr.sumPrice)),2) asave, #实际节约
  33. ifnull(ROUND(1+(( sum(rr.monthLimit)/rr.sumDay*rr.realDay-(sum(rr.sumPrice)))/(sum(rr.monthLimit)/rr.sumDay*rr.realDay)),2),0) realReason,#实际达成率
  34. IFNULL(ROUND(sum(rr.monthBudget)/sum(rr.cowSumBudgetMonth),2),0) oneCowBudget, #单头牛预算
  35. -- IFNULL(ROUND(sum(rr.monthLimit)/sum(rr.cowSumBudgetMonth),2),0) oneCowLimit, #单头牛内控
  36. sum(rr.oneCowLimit) oneCowLimit,
  37. -- IFNULL(ROUND((sum(rr.monthLimit)/rr.sumDay*rr.realDay)/(sum(rr.cowSumBudgetMonth)/rr.sumDay*rr.realDay),2),0) oneCowQmonthBudget,
  38. sum(rr.oneCowQmonthBudget) oneCowQmonthBudget,
  39. #单头牛迄今内控
  40. sum(rr.oneCowSumPrice) oneCowSumPrice,
  41. -- IFNULL(ROUND(sum(rr.sumPrice)/(sum(rr.cowSum)/rr.sumDay*rr.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  42. -- IFNULL(ROUND((sum(rr.monthLimit)/rr.sumDay*rr.realDay-(sum(rr.sumPrice)))/(sum(rr.cowSum)/rr.sumDay*rr.realDay),2),0) oneCowAsave,
  43. sum(rr.oneCowAsave)oneCowAsave,
  44. 18 pastureId
  45. FROM
  46. (SELECT
  47. '维修费(万元)' ftype,
  48. ROUND(qq.monthBudget,2) monthBudget, #预算
  49. ROUND(qq.monthLimit,2) monthLimit, #内控
  50. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  51. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  52. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  53. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  54. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  55. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  56. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  57. IFNULL(ROUND(qq.sumPrice/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  58. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  59. qq.sumDay,
  60. qq.realDay,
  61. qq.cowSumBudgetMonth,
  62. qq.cowSum
  63. FROM
  64. (SELECT IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  65. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='维修费' AND (SELECT iscal FROM department d WHERE d.id=month_budget.`deptId`)= 0),0)monthBudget,
  66. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  67. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='维修费' AND (SELECT iscal FROM department d WHERE d.id=month_budget.`deptId`)= 0 ),0)monthLimit,
  68. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  69. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='维修费' AND (SELECT iscal FROM department d WHERE d.id=month_budget.`deptId`)= 0 ),0)cowSumBudgetMonth,
  70. 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,
  71. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  72. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  73. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  74. 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,
  75. tem.sumPrice
  76. FROM(SELECT
  77. ROUND(IFNULL(SUM( pu.`sumPrice` - pu.`quitNumber` * pu.`price`),0),2) sumPrice,
  78. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  79. 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
  80. FROM
  81. partuse pu
  82. INNER JOIN bigpartuse bpu
  83. ON pu.`bigId` = bpu.`id`
  84. WHERE bpu.`useType` <> 3 AND bpu.`receiveTime` >= CONCAT(DATE_FORMAT(DATE_ADD(?,INTERVAL -1 DAY),'%Y-%m'),'-01')
  85. AND bpu.`receiveTime` < CONCAT(DATE_FORMAT(DATE_ADD(DATE_ADD(?,INTERVAL -1 DAY),INTERVAL 1 YEAR),'%Y-%m'),'-01')
  86. AND (
  87. pu.`useTypeV` = '维修'
  88. OR pu.`useTypeV` = '保养'
  89. ) AND (SELECT iscal FROM department d WHERE d.id=bpu.`departmentId`)= 0
  90. ) tem) qq
  91. UNION ALL
  92. SELECT
  93. '电费(万元)' ftype,
  94. ROUND(qq.monthBudget,2) monthBudget, #预算
  95. ROUND(qq.monthLimit,2) monthLimit, #内控
  96. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  97. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  98. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  99. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  100. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  101. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  102. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  103. IFNULL(ROUND(qq.sumPrice/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  104. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  105. qq.sumDay,
  106. qq.realDay,
  107. qq.cowSum,
  108. qq.cowSumBudgetMonth
  109. FROM (SELECT IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  110. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='电费'),0)monthBudget,
  111. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  112. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='电费' ),0)monthLimit,
  113. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  114. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='电费' ),0)cowSumBudgetMonth,
  115. 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,
  116. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  117. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  118. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  119. 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,
  120. tem.sumPrice
  121. FROM(SELECT
  122. ROUND(IFNULL(SUM(e.elecConsumption * e.`price`),0),2) sumPrice,
  123. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  124. 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
  125. FROM
  126. electricity e
  127. INNER JOIN `measure` m
  128. ON m.id = e.measureId
  129. WHERE m.meterType = '电表' AND m.useType='2级表' AND e.date >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  130. AND e.date < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  131. ) tem) qq
  132. UNION ALL
  133. SELECT
  134. '水费(万元)' ftype,
  135. ROUND(qq.monthBudget,2) monthBudget, #预算
  136. ROUND(qq.monthLimit,2) monthLimit, #内控
  137. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  138. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  139. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  140. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  141. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  142. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  143. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  144. IFNULL(ROUND(qq.sumPrice/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  145. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  146. qq.sumDay,
  147. qq.realDay,
  148. qq.cowSum,
  149. qq.cowSumBudgetMonth
  150. FROM (SELECT IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  151. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='水费'),0)monthBudget,
  152. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  153. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='水费' ),0)monthLimit,
  154. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  155. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='水费' ),0)cowSumBudgetMonth,
  156. 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,
  157. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  158. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  159. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  160. 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,
  161. tem.sumPrice
  162. FROM(SELECT
  163. ROUND(IFNULL(SUM(e.waterConsumption * e.`price`),0),2) sumPrice,
  164. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  165. 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
  166. FROM
  167. water e
  168. INNER JOIN `measure` m
  169. ON m.id = e.measureId
  170. WHERE m.meterType = '水表' AND m.useType='2级表' AND e.date >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  171. AND e.date < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  172. ) tem) qq
  173. UNION ALL
  174. SELECT
  175. '燃动费(万元)' ftype,
  176. ROUND(qq.monthBudget,2) monthBudget, #预算
  177. ROUND(qq.monthLimit,2) monthLimit, #内控
  178. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  179. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  180. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  181. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  182. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  183. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  184. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  185. IFNULL(ROUND(qq.sumPrice/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  186. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  187. qq.sumDay,
  188. qq.realDay,
  189. qq.cowSum,
  190. qq.cowSumBudgetMonth
  191. FROM (SELECT IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  192. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='燃动费'),0)monthBudget,
  193. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  194. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='燃动费' ),0)monthLimit,
  195. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  196. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='燃动费' ),0)cowSumBudgetMonth,
  197. 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,
  198. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  199. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  200. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  201. 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,
  202. tem.sumPrice
  203. FROM(SELECT
  204. ROUND(IFNULL(SUM(d.oilAmount * d.`price`),0),2) sumPrice,
  205. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  206. 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
  207. FROM
  208. `diesel` d
  209. WHERE d.selTime >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  210. AND d.selTime < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  211. ) tem) qq) rr
  212. UNION ALL
  213. SELECT
  214. p.name,
  215. p.sort,
  216. '设备指标(万元)'ftype,
  217. ROUND(sum(rr.monthBudget),2) monthBudget, #预算
  218. ROUND(sum(rr.monthLimit),2) monthLimit, #内控
  219. ROUND(sum(rr.sumPrice),2) sumPrice, #迄今实际
  220. ROUND( sum(rr.monthLimit)/rr.sumDay*rr.realDay,2) qmonthBudget, #迄今内控
  221. ROUND( sum(rr.monthLimit)/rr.sumDay*rr.realDay-(sum(rr.sumPrice)),2) asave, #实际节约
  222. ifnull(ROUND(1+(( sum(rr.monthLimit)/rr.sumDay*rr.realDay-(sum(rr.sumPrice)))/(sum(rr.monthLimit)/rr.sumDay*rr.realDay)),2),0) realReason,#实际达成率
  223. IFNULL(ROUND(sum(rr.monthBudget)/sum(rr.cowSumBudgetMonth),2),0) oneCowBudget, #单头牛预算
  224. IFNULL(ROUND(sum(rr.monthLimit)/sum(rr.cowSumBudgetMonth),2),0) oneCowLimit, #单头牛内控
  225. IFNULL(ROUND((sum(rr.monthLimit)/rr.sumDay*rr.realDay)/(sum(rr.cowSumBudgetMonth)/rr.sumDay*rr.realDay),2),0) oneCowQmonthBudget, #单头牛迄今内控
  226. IFNULL(ROUND(sum(rr.sumPrice)/(sum(rr.cowSum)/rr.sumDay*rr.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  227. IFNULL(ROUND((sum(rr.monthLimit)/rr.sumDay*rr.realDay-(sum(rr.sumPrice)))/(sum(rr.cowSum)/rr.sumDay*rr.realDay),2),0) oneCowAsave,
  228. pastureId
  229. FROM
  230. (SELECT
  231. '维修费(万元)' ftype,
  232. ROUND(qq.monthBudget,2) monthBudget, #预算
  233. ROUND(qq.monthLimit,2) monthLimit, #内控
  234. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  235. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  236. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  237. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  238. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  239. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  240. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  241. IFNULL(ROUND(qq.sumPrice/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  242. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSum/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  243. qq.sumDay,
  244. qq.realDay,
  245. qq.cowSumBudgetMonth,
  246. qq.cowSum,
  247. qq.pastureId
  248. FROM
  249. (SELECT tem.pastureId,IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  250. 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,
  251. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  252. 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,
  253. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  254. 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,
  255. 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,
  256. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  257. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  258. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  259. 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,
  260. tem.sumPrice
  261. FROM(SELECT
  262. bpu.pastureId,
  263. ROUND(IFNULL(SUM( pu.`sumPrice` - pu.`quitNumber` * pu.`price`),0),2) sumPrice,
  264. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  265. 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
  266. FROM
  267. partuse pu
  268. INNER JOIN bigpartuse bpu
  269. ON pu.`bigId` = bpu.`id`
  270. WHERE bpu.`useType` <> 3 AND bpu.`receiveTime` >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  271. AND bpu.`receiveTime` < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  272. AND (
  273. pu.`useTypeV` = '维修'
  274. OR pu.`useTypeV` = '保养'
  275. ) AND (SELECT iscal FROM department d WHERE d.id=bpu.`departmentId`)= 0
  276. AND (bpu.pastureId = ? OR ? in ('18',''))
  277. AND (bpu.departmentId = ? or ? = '')
  278. GROUP BY bpu.pastureId ) tem) qq
  279. UNION ALL
  280. SELECT
  281. '电费(万元)' ftype,
  282. ROUND(qq.monthBudget,2) monthBudget, #预算
  283. ROUND(qq.monthLimit,2) monthLimit, #内控
  284. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  285. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  286. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  287. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  288. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  289. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  290. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  291. IFNULL(ROUND(qq.sumPrice/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  292. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  293. qq.sumDay,
  294. qq.realDay,
  295. qq.cowSumBudgetMonth,
  296. qq.cowSum,
  297. qq.pastureId
  298. FROM (SELECT
  299. tem.pastureId,
  300. IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  301. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='电费' AND pastureId = tem.pastureId),0)monthBudget,
  302. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  303. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='电费' AND pastureId = tem.pastureId ),0)monthLimit,
  304. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  305. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='电费' AND pastureId = tem.pastureId ),0)cowSumBudgetMonth,
  306. 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,
  307. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  308. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  309. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  310. 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,
  311. tem.sumPrice
  312. FROM(SELECT
  313. m.pastureId,
  314. ROUND(IFNULL(SUM(e.elecConsumption * e.`price`),0),2) sumPrice,
  315. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  316. 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
  317. FROM
  318. electricity e
  319. INNER JOIN `measure` m
  320. ON m.id = e.measureId
  321. WHERE m.meterType = '电表' AND m.useType='2级表' AND e.date >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  322. AND e.date < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  323. AND (m.pastureId = ? OR ? in ('18',''))
  324. AND (m.departmentId = ? or ? = '')
  325. GROUP BY m.pastureId ) tem) qq
  326. UNION ALL
  327. SELECT
  328. '水费(万元)' ftype,
  329. ROUND(qq.monthBudget,2) monthBudget, #预算
  330. ROUND(qq.monthLimit,2) monthLimit, #内控
  331. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  332. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  333. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  334. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  335. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  336. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  337. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  338. IFNULL(ROUND(qq.sumPrice/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  339. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  340. qq.sumDay,
  341. qq.realDay,
  342. qq.cowSumBudgetMonth,
  343. qq.cowSum,
  344. qq.pastureId
  345. FROM (SELECT
  346. tem.pastureId,
  347. IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  348. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='水费' AND pastureId = tem.pastureId),0)monthBudget,
  349. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  350. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='水费' AND pastureId = tem.pastureId ),0)monthLimit,
  351. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  352. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='水费' AND pastureId = tem.pastureId ),0)cowSumBudgetMonth,
  353. 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,
  354. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  355. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  356. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  357. 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,
  358. tem.sumPrice
  359. FROM(SELECT
  360. m.pastureId,
  361. ROUND(IFNULL(SUM(e.waterConsumption * e.`price`),0),2) sumPrice,
  362. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  363. 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
  364. FROM
  365. water e
  366. INNER JOIN `measure` m
  367. ON m.id = e.measureId
  368. WHERE m.meterType = '水表' AND m.useType='2级表' AND e.date >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  369. AND e.date < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  370. AND (m.pastureId = ? OR ? in ('18',''))
  371. AND (m.departmentId = ? or ? = '')
  372. GROUP BY m.pastureId ) tem) qq
  373. UNION ALL
  374. SELECT
  375. '燃动费(万元)' ftype,
  376. ROUND(qq.monthBudget,2) monthBudget, #预算
  377. ROUND(qq.monthLimit,2) monthLimit, #内控
  378. ROUND(qq.sumPrice/10000,2) sumPrice, #迄今实际
  379. ROUND( qq.monthLimit/qq.sumDay*qq.realDay,2) qmonthBudget, #迄今内控
  380. ROUND( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000),2) asave, #实际节约
  381. ROUND(1+(( qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice/10000))/(qq.monthLimit/qq.sumDay*qq.realDay)),2) realReason,#实际达成率
  382. IFNULL(ROUND(qq.monthBudget/qq.cowSumBudgetMonth*10000,2),0) oneCowBudget, #单头牛预算
  383. IFNULL(ROUND(qq.monthLimit/qq.cowSumBudgetMonth*10000,2),0) oneCowLimit, #单头牛内控
  384. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay)/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay)*10000,2),0) oneCowQmonthBudget, #单头牛迄今内控
  385. IFNULL(ROUND(qq.sumPrice/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowSumPrice,#单头牛的迄今实际
  386. IFNULL(ROUND((qq.monthLimit/qq.sumDay*qq.realDay-(qq.sumPrice))/(qq.cowSumBudgetMonth/qq.sumDay*qq.realDay),2),0) oneCowAsave,#单头牛迄今实际节约
  387. qq.sumDay,
  388. qq.realDay,
  389. qq.cowSumBudgetMonth,
  390. qq.cowSum,
  391. qq.pastureId
  392. FROM (SELECT
  393. tem.pastureId,
  394. IFNULL((SELECT SUM(monthBudget)monthBudget FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  395. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='燃动费' AND pastureId = tem.pastureId),0)monthBudget,
  396. IFNULL((SELECT SUM(monthLimit)monthLimit FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  397. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='燃动费' AND pastureId = tem.pastureId ),0)monthLimit,
  398. IFNULL((SELECT SUM(cowSumBudgetMonth)cowSumBudgetMonth FROM `month_budget` WHERE concat(budgetMonth,'-','01') >= tem.startTime
  399. AND concat(budgetMonth,'-','01') < tem.endTime AND feeType='燃动费' AND pastureId = tem.pastureId ),0)cowSumBudgetMonth,
  400. 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,
  401. #DATEDIFF(DATE_ADD(LAST_DAY(tem.endTime),INTERVAL 1 DAY),tem.startTime) sumDay,#当前总天数
  402. #DATEDIFF(tem.endTime,tem.startTime) realDay,#到当前选择的天数
  403. DATEDIFF(LAST_DAY(tem.endTime),CONCAT(DATE_FORMAT(tem.endTime,'%Y-%m'),'-01'))+1 sumDay,
  404. 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,
  405. tem.sumPrice
  406. FROM(SELECT
  407. d.pastureId,
  408. ROUND(IFNULL(SUM(d.oilAmount * d.`price`),0),2) sumPrice,
  409. CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01') startTime,
  410. 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
  411. FROM
  412. `diesel` d
  413. WHERE d.selTime >= CONCAT(DATE_FORMAT(CONCAT(LEFT(?,7),'-01'),'%Y-%m'),'-01')
  414. AND d.selTime < CONCAT(DATE_FORMAT(DATE_ADD(CONCAT(LEFT(?,7),'-01'),INTERVAL 1 MONTH),'%Y-%m'),'-01')
  415. AND (d.pastureId = ? OR ? in ('18',''))
  416. AND (d.deptId = ? or ? = '')
  417. GROUP BY d.pastureId ) tem) qq)rr
  418. INNER JOIN pasture p
  419. ON p.id = rr.pastureId
  420. GROUP BY rr.pastureId
  421. )t
  422. left join
  423. pasture pp on pp.id = t.pastureId
  424. where pp.id <>18
  425. group by pp.center,pp.region,pp.name with rollup