Oracle / PLSQL递归

问题描述 投票:0回答:1

我有一张桌子,如:

+------+------+--------+------+
|      |      |  UOM   |      |
| ITEM | UOM  | BEFORE | SOLD |
+------+------+--------+------+
|  1   | Case |  Box   |  Y   |
|  2   | Each |  Oz    |      |
|  1   | Box  |  Each  |  Y   |
|  1   | Each |  Oz    |      |
|  2   | Case |  Box   |  Y   |
|  2   | Box  |  Each  |  Y   |
+------+------+--------+------+

如何查询它,得到类似以下结果:

+------+-------+------+
| ITEM |  UOM  | SOLD |
+------+-------+------+
|  1   | Case  |  Y   |
|  1   | Box   |  Y   |
|  1   | Each  |  Y   |
|  1   | Oz    |      |
|  2   | Case  |  Y   |
|  2   | Box   |  Y   |
|  2   | Each  |  Y   |
|  2   | Oz    |      |
+------+-------+------+

列出商品销售的每个计量单位,从最高UOM降到最低UOM,其中出售的商品为空?

感谢您的帮助!

recursion plsql
1个回答
0
投票

无需递归。 UNION和外部联接将起作用。

SELECT a.item, a.uom, b.sold
FROM
  (SELECT item, uom
   FROM uom_temp
   UNION
   SELECT item, uom_before
   FROM uom_temp) a,
  (SELECT * FROM (
     SELECT item, uom, sold
     FROM uom_temp
     UNION
     SELECT item, uom_before uom, sold
     FROM uom_temp)
   WHERE sold IS NOT NULL) b
WHERE a.item = b.item (+)
AND a.uom = b.uom (+)
ORDER BY a.item, b.sold;
热门问题
推荐问题
最新问题