1
我有5個區域,每個區域包含1個商店和1個倉庫。我有一張包含區域和相應的商店和倉庫的表格。我也有一張表,列出了商店和倉庫的庫存。最後,有一張列出倉庫允許數量的表格。如果我在庫存表看,現在它會這樣說:連接和移除笛卡爾積
FACILITY ID ITEM_NUMBER QTY
STORE 1 15D 2
WAREHOUSE 1 15D 1
的問題是,商店1倉庫1屬於區域1,我想看看它的格式如下:
REGION WAREHOUSE QTY OH STORE QTY OH
1 1 2 1 1
等等......所以我寫了下面的SQL:
Create or replace view REGION_BALANCES
as
with WAREHOUSES as
(select A.REGION_CODE, A.REG_DESC, A.WAREHOUSE_NUMBER, A.FACILITY_ID,
C.ITEM_NUMBER, sum(C.IN_STOCK_QTY) as IN_STOCK_QTY,
B.ALLOWED_QTY
from REG_WHS_STR_ASSOC A join
ALLOWANCES B on (A.FACILITY_ID = B.FACILITY_ID) join
INVENTORIES C on (A.FACILITY_ID = FACILITY_ID) and (B.ITEM_NUMBER = C.ITEM_NUMBER)
group by A.REGION_CODE, A.REG_DESC, A.WAREHOUSE_NUMBER, C.ITEM_NUMBER, B.ALLOWED_QTY),
STORES as
(select A.REGION_CODE, A.REG_DESC, A.STORE_NUMBER, A.FACILITY_ID, B.ITEM_NUMBER,
sum(B.IN_STOCK_QTY) as IN_STOCK_QTY
from REG_WHS_STR_ASSOC A join
INVENTORIES B on (A.FACILITY_ID = B.FACILITY_ID)
group by A.REGION_CODE, A.REG_DESC, A.STORE_NUMBER, B.ITEM_NUMBER
)
select A.REGION_CODE, A.REG_DESC, A.WAREHOUSE_NUMBER, A.FACILITY_ID,
WAREHOUSES.ITEM_NUMBER, WAREHOUSES.IN_STOCK_QTY, WAREHOUSES.ALLOWED_QTY,
STORES.STORE_NUMBER, STORES.FACILITY_ID, STORES.ITEM_NUMBER, STORES.IN_STOCK_QTY
from REG_WHS_STR_ASSOC A join
WAREHOUSES on (A.REGION_CODE = WAREHOUSES.REGION_CODE) join
STORES on (A.REGION_CODE = STORES.REGION_CODE)
order by 5,1 asc;
我已經改變了我的各地加盟左聯接到右連接到交叉連接到內部連接;然而,我要麼得到30條記錄(並且我需要數百條記錄),要麼我得到笛卡爾產品。任何人都可以提供意見,我做錯了什麼?
重新格式化查詢以使其可讀後,我看到第9行的語法被破壞: (A.FACILITY_ID = FACILITY_ID)和B.ITEM_NUMBER = C.ITEM_NUMBER)加入INVENTORIES C。 「和」應該是「加入」條款的一部分嗎?最後還有一個額外的權利。 – dg99
對不起,我忘記了。是的,它應該有第一個括號。我在這裏犯了錯誤,而不是TOAD。我會解決這個問題。謝謝你指出。聲明在蟾蜍身上運行;然而,它要麼給我一個可笑的少量記錄或笛卡爾積。 –
你可以設置一個sqlfiddle嗎? – terary