0

In Postgresql, the query is getting an error for saying that schema "purchase_order" doesn't exist.

from ((select a.item_no
    from stocka a
        join order_item oi
            on (oi.item_no = a.item_no)
        join purchase_order po
            on (po.order_no = oi.order_no)
    where po.location = 'LocationA'
    ) UNION ALL
    (select b.item_no
    from stockb b
        join order_item oi
            on (oi.item_no = b.item_no)
        join purchase_order po
            on (po.order_no = oi.order_no)
    where po.location = 'LocationB'
    ))

The Union is for the from clause

It is for some reason saying that purchase_order isn't a table, but a schema.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Padagomez
  • 1,114
  • 5
  • 14
  • 35
  • Also post the output of `SHOW search_path;` please - and (as always) your version of Postgres. Plus, a ***verbatim*** copy of the error message. – Erwin Brandstetter May 21 '14 at 20:49

1 Answers1

1

The error you describe is not due to the code you posted, which should work - given the objects exist.

I only added an alias for the subquery: sub (required!), simplified with USING (optional), removed redundant parentheses and reformatted:

SELECT *
FROM  (
   SELECT a.item_no
   FROM   stocka a
   JOIN   order_item     oi USING (item_no)
   JOIN   purchase_order po USING (order_no)
   WHERE  po.location = 'LocationA'

   UNION ALL
   SELECT b.item_no
   FROM   stockb b
   JOIN   order_item     oi USING (item_no)
   JOIN   purchase_order po USING (order_no)
   WHERE  po.location = 'LocationB'
   ) sub;

Depending on db layout (table definitions?) and exact requirements this could possibly be simplified further.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228