t.sql 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. select t.*,( reportery-(sumLaid+sumquit-sumuse-sumrefund) ) diff from
  2. (
  3. select storage.*,
  4. ifnull(laid.sumLaid,0) sumLaid,
  5. ifnull(u.sumuse,0) sumuse,
  6. ifnull(quit.sumquit,0) sumquit,
  7. ifnull(refund.sumrefund,0) sumrefund
  8. from
  9. (
  10. SELECT pr.contractid,pr.partcode, pr.reportery
  11. FROM part_repertory pr
  12. join warehouse w on w.id = pr.locationId
  13. WHERE w.`pastureId` = 31
  14. )storage
  15. left join
  16. (
  17. SELECT pr.contractid,sum(pl.storageAmount )sumLaid
  18. FROM part_repertory pr
  19. join warehouse w on w.id = pr.locationId
  20. left join partlaid pl on pr.contractId =pl.contractId and pr.locationId = pl.locationId
  21. WHERE w.`pastureId` = 31
  22. group by pr.contractId
  23. )laid on storage.contractId = laid.contractId
  24. left join
  25. (
  26. SELECT pr.contractid,sum(pu.checkoutNumber) sumuse
  27. FROM part_repertory pr
  28. join warehouse w on w.id = pr.locationId
  29. left join partuse pu on pr.contractId =pu.contractId and pr.locationId = pu.locationId
  30. WHERE w.`pastureId` = 31
  31. group by pr.contractId
  32. )u on storage.contractId = u.contractid
  33. left join
  34. (
  35. SELECT pr.contractid ,sum(pq.quitNumber)sumquit
  36. FROM part_repertory pr
  37. join warehouse w on w.id = pr.locationId
  38. left join partquit pq on pr.contractId =pq.contractId
  39. join bigpartquit bpq on bpq.id =pq.bigid and bpq.pastureid =w.pastureId
  40. WHERE w.`pastureId` = 31
  41. group by pr.contractId
  42. )quit on storage.contractid =quit.contractid
  43. left join
  44. (
  45. SELECT pr.contractid,sum(pf.refundNumber)sumrefund
  46. FROM part_repertory pr
  47. join warehouse w on w.id = pr.locationId
  48. left join partrefund pf on pr.contractId =pf.contractId
  49. join bigpartrefund bpf on pf.bigid =bpf.id and bpf.pastureid =w.pastureId
  50. WHERE w.`pastureId` = 31
  51. group by pr.contractId
  52. )refund on refund.contractId =storage.contractId
  53. )t