pro.sql 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. SELECT psum,eqcodes,eqClassName,eqClassName typeName,
  2. ifnull(round(psum/eqcodes,2),0.00) avgsum,pastureName,departmentName
  3. FROM(
  4. SELECT
  5. ROUND(IFNULL(SUM( pu.`sumPrice` - pu.`quitNumber` * pu.`price`),0),2) psum,
  6. COUNT(DISTINCT IF(bpa.`RUCode`='',NULL,bpa.`RUCode`)) eqcodes,
  7. COUNT(DISTINCT pu.`bigid`) eqcodes2,
  8. IFNULL( (SELECT typeName FROM eqclass WHERE id = e.`eqClassId`),'无类别')eqClassName,
  9. p.`name` pastureName,
  10. dp.`name` departmentName
  11. FROM
  12. partuse pu
  13. INNER JOIN bigpartuse bpu
  14. ON pu.`bigId` = bpu.`id`
  15. left join pasture p
  16. on p.id =bpu.pastureId
  17. left join department dp
  18. on dp.id = bpu.departmentId
  19. LEFT JOIN `bigpartapply` bpa ON bpa.`applyCode` = bpu.`oddCode`
  20. LEFT JOIN equipment e
  21. ON (e.`eqCode` = pu.`eqCode` or e.id=pu.eqId)
  22. WHERE bpu.`useType` <> 3 AND
  23. date_format(bpu.receiveTime,'%Y-%m') >=concat_ws('-',?,?)
  24. AND (
  25. pu.`useTypeV` IN ('维修','保养')
  26. )
  27. GROUP BY e.`eqClassId`,bpu.`pastureId`,bpu.`departmentId`
  28. )tem
  29. WHERE tem.eqClassName = ? OR ? = ''
  30. order by tem.psum desc,typeName