Domanda

I have 4 tables.

create table LOCATION(
loccode Number(2) NOT NULL,
locname Varchar2 (30),
state Varchar2 (10),
population Number(7),
travelcode Number(1),
CONSTRAINT PK_LOCATION PRIMARY KEY (loccode)
);

create table DEPOT(
depid Number(4) NOT NULL,
audit_year Number(4),
address Varchar2 (30),
mgrname Varchar2 (30),
capacity Number(7),
capacity_used Number(7),
mgrgender Varchar2(10),
loccode Number(2) NOT NULL,
CONSTRAINT PK_DEPOT PRIMARY KEY (depid),
CONSTRAINT FK_LOCATION FOREIGN KEY (loccode) REFERENCES LOCATION

create table MANUFACTURER(
manid Number (2) NOT NULL,
manname Varchar2(30),
loccode Number(2) NOT NULL,
CONSTRAINT PK_MANUFACTURER PRIMARY KEY (manid),
CONSTRAINT FK_LOC FOREIGN KEY (loccode) REFERENCES LOCATION
);
create table STOCKITEM(
stkid Number(2) NOT NULL,
stkname Varchar2(30),
sellingprice Number(6,2),
purchaseprice Number(6,2),
depid Number(4) NOT NULL,
manid Number(2) NOT NULL,
CONSTRAINT PK_STOCKITEM PRIMARY KEY (stkid),
CONSTRAINT FK_DEP FOREIGN KEY (depid) REFERENCES DEPOT,
CONSTRAINT FK_MANUFACTURER FOREIGN KEY (manid) REFERENCES MANUFACTURER 
);

I am trying to generate a query that lets me see the stock id, stock name, manufacturer location and department location.

This requires 4 inner join clauses.

1 to link stockitem to depot, one to link stockitem to manufacturer, and two unique ones linking location to both depot and manufacturer.

Im using this at the moment;

select  
    stockitem.stkid as "STOCK ID",
    stockitem.stkname as "STOCK NAME", 
    manloc.locname as "MANUFACTURER LOCATION", 
    deploc.locname as "DEPOT LOCATION"  
from stockitem
inner join depot on stockitem.depid = depot.depid
inner join manufacturer on stockitem.manid = manufacturer.manid
inner join location  on location.locid = deploc.locid as deploc
inner join location  on location.locid = manloc.locid as manloc
order by stockitem.stkid asc;

however it is spitting back that the string is not properly terminated. Am I missing something here?

If I put the alias's before the 'on' (the deploc and manloc), then I get an error saying that its missing an expected operator...i.e....'on'.

Where am I going wrong with this?

È stato utile?

Soluzione

The location table alias's should be after the table name and there should be no AS when aliasing tables (that's an Oracle quirk):

select  
stockitem.stkid as "STOCK ID",
stockitem.stkname as "STOCK NAME", 
manloc.locname as "MANUFACTURER LOCATION", 
deploc.locname as "DEPOT LOCATION"  
from stockitem
inner join depot on stockitem.depid = depot.depid 
inner join manufacturer on stockitem.manid = manufacturer.manid 
inner join location deploc on depot.loccode = deploc.loccode 
inner join location manloc on manufacturer.loccode = manloc.loccode 
order by stockitem.stkid asc;

You don't actually need the AS when aliasing columns but it makes it read easier.

Note the correct column reference in the location inner joins

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top