SELECT * FROM INVENTORY_LOCATION;
Result -->
+-------+--------+-----------+-------+
| Invt | Doc_no | Location | Qty |
+-------+--------+-----------+-------+
| 00X7X | 5123 | Warehous1 |-123CS |
| 00X7X | 5123 | Warehous2 | 123CS |
| 00N6U | 5345 | Warehous5 |-456CS |
| 00N6U | 5345 | Warehous6 | 456CS |
+-------+--------+-----------+-------+
Desired Result -->
+-------+--------+---------------+-------------+-------+
| Invt | Doc_no | From Location | To Location | Qty |
+-------+--------+---------------+-------------+-------+
| 00X7X | 5123 | Warehous1 | Warehous2 | 123CS |
| 00N6U | 5345 | Warehous5 | Warehous6 | 456CS |
+-------+--------+---------------+-------------+-------+
如您所见,我有一个表格,显示库存的位置。它离开一个仓库,因此在该位置的数量中显示负数。然后它会转到一个新位置,并在该位置的数量中显示正数。
我希望进入一条线,包括物品来自的位置,以及它的位置。
请有人帮助我获得理想的结果吗?
提前致谢
答案 0 :(得分:0)
这看起来像你试图转向INVT和DOC_NO?
SELECT
INVT,
DOC_NO,
MAX(CASE WHEN QTY LIKE '-%' THEN LOCATION END) AS "FROM LOCATION",
MAX(CASE WHEN QTY NOT LIKE '-%' THEN LOCATION END) AS "TO LOCATION",
MAX(QTY) AS "Qty"
FROM
INVENTORY_LOCATION
GROUP BY
INVT,
DOC_NO
答案 1 :(得分:0)
这是一种方法。我使用了两个派生表。一个qty以-
开头而另一个没有。加入这两个表来获取from和to位置。我这样做的唯一原因是你的数字字段似乎不是你提供的数据中的数字。
<强> SQL Fiddle Demo 强>
select fromLoc.invt,
fromLoc.doc_no,
fromLoc.location as "From Location",
toLoc.location as "To Locaiton",
toLoc.qty
from
(select * from INVENTORY_LOCATION where qty like '-%') fromLoc
join
(select * from INVENTORY_LOCATION where qty not like '-%') toLoc
on fromLoc.invt = toLoc.invt and fromLoc.doc_no = toLoc.doc_no