Advertisement
Pandaaaa906

U8库存批次单位成本

Dec 7th, 2022
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. with inv_history as (
  2. SELECT
  3. *
  4. FROM (
  5. SELECT
  6. inv_pos.cvouchtype,
  7. inv_pos.cWhCode,
  8. inv_pos.cPosCode,
  9. inv_pos.cInvCode,
  10. inv_pos.cBatch,
  11. inv_pos.cFree1,
  12. rd01_d.iQuantity,
  13. rd01_d.iPrice,
  14. rd01_d.iPrice / rd01_d.iQuantity unit_price,
  15. case when iPOsID is null then rd01_d.iQuantity else sum(rd01_d.iQuantity) over(partition by iPOsID) end total_qty
  16. FROM rdrecords01 rd01_d
  17. INNER JOIN [dbo].[InvPosition] inv_pos
  18. ON rd01_d.AutoID = inv_pos.RdsID
  19. AND inv_pos.cvouchtype = '01'
  20. ) tmp
  21. WHERE tmp.total_qty>0
  22.  
  23. union
  24.  
  25. SELECT
  26. inv_pos.cvouchtype,
  27. inv_pos.cWhCode,
  28. inv_pos.cPosCode,
  29. inv_pos.cInvCode,
  30. inv_pos.cBatch,
  31. inv_pos.cFree1,
  32. rd_d.iQuantity,
  33. case rd_d.iPrice when 0 then COALESCE(rd_d.cDefine26/rd_d.iQuantity, 0) else rd_d.iPrice end iPrice,
  34. case rd_d.iPrice when 0 then COALESCE(rd_d.cDefine26/rd_d.iQuantity, 0) else rd_d.iPrice end / rd_d.iQuantity unit_price,
  35. rd_d.iQuantity total_qty
  36.  
  37. FROM [dbo].[InvPosition] inv_pos
  38. LEFT JOIN rdrecords34 rd_d
  39. ON rd_d.AutoID = inv_pos.RdsID
  40. WHERE inv_pos.cvouchtype = '34'
  41. AND inv_pos.iQuantity > 0
  42.  
  43. union
  44.  
  45. SELECT
  46. inv_pos.cvouchtype,
  47. inv_pos.cWhCode,
  48. inv_pos.cPosCode,
  49. inv_pos.cInvCode,
  50. inv_pos.cBatch,
  51. inv_pos.cFree1,
  52. rd_d.iQuantity,
  53. case rd_d.iPrice when 0 then COALESCE(rd_d.cDefine26/rd_d.iQuantity, 0) else rd_d.iPrice end iPrice,
  54. case rd_d.iPrice when 0 then COALESCE(rd_d.cDefine26/rd_d.iQuantity, 0) else rd_d.iPrice end / rd_d.iQuantity unit_price,
  55. rd_d.iQuantity total_qty
  56.  
  57. FROM [dbo].[InvPosition] inv_pos
  58. LEFT JOIN rdrecords08 rd_d
  59. ON rd_d.AutoID = inv_pos.RdsID
  60. WHERE inv_pos.cvouchtype = '08'
  61. AND inv_pos.iQuantity > 0
  62. )
  63.  
  64. -- SELECT * FROM inv_history
  65.  
  66. SELECT
  67. cInvCode,
  68. cBatch,
  69. cFree1,
  70. count(distinct cPosCode) position_count,
  71. count(*) stock_count,
  72. min(unit_price) min_unit_price,
  73. max(unit_price) max_unit_price,
  74. avg(unit_price) unit_price
  75. FROM inv_history
  76.  
  77. GROUP BY cInvCode, cBatch, cFree1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement