t2.sql 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. select * from (
  2. select t.*,( reportery-(sumLaid+sumquit-sumuse-sumrefund) ) diff from
  3. (
  4. select storage.*,
  5. ifnull(laid.sumLaid,0) sumLaid,
  6. ifnull(u.sumuse,0) sumuse,
  7. ifnull(quit.sumquit,0) sumquit,
  8. ifnull(refund.sumrefund,0) sumrefund
  9. from
  10. (
  11. SELECT p.name, w.pastureid, pr.contractid,pr.partcode, pv.providername ,pr.brand, pr.reportery
  12. FROM part_repertory pr
  13. join warehouse w on w.id = pr.locationId
  14. left join pasture p on pr.pastureid =p.id
  15. left join provider pv on pr.providerid =pv.id
  16. where w.pastureId=31
  17. )storage
  18. left join
  19. (
  20. SELECT w.pastureid, pr.contractid,sum(pl.storageAmount )sumLaid
  21. FROM part_repertory pr
  22. join warehouse w on w.id = pr.locationId
  23. left join partlaid pl on pr.contractId =pl.contractId and pr.locationId = pl.locationId
  24. group by pr.contractId,w.pastureid
  25. )laid on storage.contractId = laid.contractId and storage.pastureid =laid.pastureid
  26. left join
  27. (
  28. SELECT w.pastureid, pr.contractid,sum(pu.checkoutNumber) sumuse
  29. FROM part_repertory pr
  30. join warehouse w on w.id = pr.locationId
  31. left join partuse pu on pr.contractId =pu.contractId and pr.locationId = pu.locationId
  32. group by pr.contractId,w.pastureid
  33. )u on storage.contractId = u.contractid and storage.pastureid =u.pastureid
  34. left join
  35. (
  36. SELECT w.pastureid, pr.contractid ,sum(pq.quitNumber)sumquit
  37. FROM part_repertory pr
  38. join warehouse w on w.id = pr.locationId
  39. left join partquit pq on pr.contractId =pq.contractId
  40. join bigpartquit bpq on bpq.id =pq.bigid and bpq.pastureid =w.pastureId
  41. group by pr.contractId,w.pastureid
  42. )quit on storage.contractid =quit.contractid and storage.pastureid =quit.pastureid
  43. left join
  44. (
  45. SELECT w.pastureid, 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. group by pr.contractId,w.pastureid
  51. )refund on refund.contractId =storage.contractId and storage.pastureid =refund.pastureid
  52. )t
  53. )t2 where diff <>0
  54. select