Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT * FROM (
  2. SELECT
  3. r1.name AS cliente,
  4. d.doc_number,
  5. d.date,
  6. pr.product_client_id || '-' || pr.name AS producto,
  7. t.name AS atributo_nombre,
  8. COALESCE(v.value_string::text, v.value_integer::text, v.value_double::text, v.value_date::text, v.value_timestamp::text, v.value_boolean::text, v.value_text::text) as atributo_valor,
  9. wls."name" as origen,
  10. wlt."name" as destino,
  11. wlt.morder as orden,
  12. r."name" AS autoelevadorista,
  13. vt."name" || '-' || v1.patent as vehiculo
  14. FROM soda.document_line l
  15. JOIN soda.product_instances pi ON pi.id = l.product_instance_id
  16. join soda.warehouse_locations wls on wls.warehouse_location_id = l.warehouse_source_location_id
  17. join soda.warehouse_locations wlt on wlt.warehouse_location_id = l.warehouse_location_id
  18. LEFT JOIN soda.document d ON d.document_id = l.document_id
  19. join soda.receiver r on r.receiver_id = d.lifter_driver_id
  20. join soda.vehicles v1 on v1.vehicle_id = d.vehicle_id
  21. join soda.vehicle_types vt on vt.vehicle_type_id = v1.vehicle_type_id
  22. JOIN soda.receiver r1 ON r1.receiver_id = d.customer_id
  23. LEFT JOIN soda.products pr ON pr.product_id = l.product_id
  24. LEFT JOIN soda.attributes_values v ON v.entity_class = 'ProductInstance' AND v.entity_id = pi.id
  25. LEFT JOIN soda.attributes_types_params p ON p.attributes_types_params_id = v.attributes_type_param_id
  26. LEFT JOIN soda.attributes_types t ON t.attributes_types_id = p.attributes_types_id
  27. LEFT JOIN soda.attributes_by_entity ae ON ae.attributes_type_id = t.attributes_types_id and  ae.entity_class = 'ProductInstance'
  28. WHERE 1=1
  29. AND d.type = 'ORDER_OF_MOVE'
  30. AND d.doc_number = ${numero}
  31. AND d.pos = ${pos}
  32. and ae.manual = true
  33. ) AS a
  34. UNION
  35. (SELECT
  36. r1.name AS cliente,
  37. d.doc_number,
  38. d.date,
  39. pr.product_client_id || '-' || pr.name AS producto,
  40. t.name AS atributo_nombre,
  41. '' as atributo_valor,
  42. wls."name" as origen,
  43. wlt."name" as destino,
  44. wlt.morder as orden,
  45. r."name" AS autoelevadorista,
  46. vt."name" || '-' || v1.patent as vehiculo
  47. FROM soda.document_line l
  48. JOIN soda.product_instances pi ON pi.id = l.product_instance_id
  49. join soda.warehouse_locations wls on wls.warehouse_location_id = l.warehouse_source_location_id
  50. join soda.warehouse_locations wlt on wlt.warehouse_location_id = l.warehouse_location_id
  51. LEFT JOIN soda.document d ON d.document_id = l.document_id
  52. join soda.receiver r on r.receiver_id = d.lifter_driver_id
  53. join soda.vehicles v1 on v1.vehicle_id = d.vehicle_id
  54. join soda.vehicle_types vt on vt.vehicle_type_id = v1.vehicle_type_id
  55. JOIN soda.receiver r1 ON r1.receiver_id = d.customer_id
  56. LEFT JOIN soda.products pr ON pr.product_id = l.product_id
  57. LEFT JOIN soda.attributes_by_entity ae ON ae.entity_class = 'ProductInstance'
  58. LEFT JOIN soda.attributes_types t ON t.attributes_types_id = ae.attributes_type_id
  59. WHERE 1=1
  60. AND d.type = 'ORDER_OF_MOVE'
  61. AND d.doc_number = ${numero}
  62. AND d.pos = ${pos}
  63. AND ae.attributes_type_id NOT IN
  64.     (SELECT p.attributes_types_id
  65.         FROM soda.attributes_values v
  66.         LEFT JOIN soda.attributes_types_params p ON p.attributes_types_params_id = v.attributes_type_param_id
  67.         WHERE v.entity_class = 'ProductInstance' AND v.entity_id = pi.id)
  68. and ae.manual = true
  69. )
  70. UNION
  71. (SELECT
  72. r1.name AS cliente,
  73. d.doc_number,
  74. d.date,
  75. pr.product_client_id || '-' || pr.name AS producto,
  76. t.name AS atributo_nombre,
  77. COALESCE(pe.letter,'') || LPAD(pe.pos::text, 4, '0') || '-' || LPAD(pe.doc_number::text, 8, '0') AS atributo_valor,
  78. wls."name" as origen,
  79. wlt."name" as destino,
  80. wlt.morder as orden,
  81. r."name" AS autoelevadorista,
  82. vt."name" || '-' || v1.patent as vehiculo
  83. FROM soda.document_line l
  84. JOIN soda.product_instances pi ON pi.id = l.product_instance_id
  85. join soda.warehouse_locations wls on wls.warehouse_location_id = l.warehouse_source_location_id
  86. join soda.warehouse_locations wlt on wlt.warehouse_location_id = l.warehouse_location_id
  87. LEFT JOIN soda.document d ON d.document_id = l.document_id
  88. join soda.receiver r on r.receiver_id = d.lifter_driver_id
  89. join soda.vehicles v1 on v1.vehicle_id = d.vehicle_id
  90. join soda.vehicle_types vt on vt.vehicle_type_id = v1.vehicle_type_id
  91. JOIN soda.receiver r1 ON r1.receiver_id = d.customer_id
  92. LEFT JOIN soda.products pr ON pr.product_id = l.product_id
  93. LEFT JOIN soda.attributes_values v ON v.entity_class = 'ProductInstance' AND v.entity_id = pi.id
  94. LEFT JOIN soda.attributes_types_params p ON p.attributes_types_params_id = v.attributes_type_param_id
  95. LEFT JOIN soda.attributes_types t ON t.attributes_types_id = p.attributes_types_id and t."name" = 'PEDIDO DE EGRESO'
  96. JOIN soda.document pe ON pe.document_id = v.value_integer
  97. WHERE 1=1
  98. AND d.type = 'ORDER_OF_MOVE'
  99. AND d.doc_number = ${numero}
  100. AND d.pos = ${pos}
  101. )
  102. ORDER BY orden, producto, atributo_nombre;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement