The following sql query below produced this result
cust table
CUST_ID AC_NO NAME AREA SALES
---------- ---------- ------------------------- ---
C001 A30045 Smiths Heavy 1 R001
C002 A30145 Heavy jonps 1 R001
C003 A30046 dangote flour 1 R002
C004 A30047 OAU ife 2 R002
Area Table
AREA_NUMBER AREA_NM AREA_Dp
----------- ---------- ----------
1 North Leeds
2 South Newcastle
3 East Surrey
4 West London
Area_geo Table
SALE REPP_ AREAA_ID AREAM AREAMANAGER_NAME
---- ----- ---------- ----- -------------------
SG01 R001 1 R110 mandy Jay
SG02 R002 2 R110 mandy Jay
SG03 R003 3 R111 Kay sole
SG04 R003 3 R111 Kay sole
SG05 R003 3 R111 Kay sole
SG06 R001 4 R110 mandy Jay
select cust.*,
area.AREA_Nm,area.AREA_Dp
from area inner join cust on
area.area_number=customer.area
ORDER BY customer.Cust_ID;
Result
CUST_ID AC_NO NAME ADDRESS AREA SALES AREA_N AREA_Dp
---------- ---------- -------------- ------------------------- ---------- ----- ---
Ac003 A30046 dangote flour court Estate 1 R002 North Leeds
Ac004 A30047 OAU ife 4 Abanishe 2 R002 South Newcastle
my intension is to further include the Areamanager_name into the above result from the table below. the table below however has a common attribute (Area_ID) to the above result (Area)
Table Area_Geo
SALE REPP_ AREA_ID AREAM AREAMANAGER_NAME
---- ----- ---------- ----- ----------------
SG01 R001 1 R110 mandy Jay
SG02 R002 2 R110 mandy Jay
SG03 R003 3 R111 Kay sole
SG04 R003 3 R111 Kay sole
SG05 R003 3 R111 Kay sole
SG06 R001 4 R110 mandy Jay
expected result
CUST_ID AC_NO NAME ADDRESS AREA SALES AREA_N AREA_dp Areamanager
---------- ---------- -------------- ------------------------- ---------- ----- ---
Ac003 A30046 dangote flour court Estate 1 R002 North Leeds mandy Jay
Ac004 A30047 OAU ife 4 Abanishe 2 R002 South Newcastle mandy Jay
I think the below should do it, I reformatted it a bit so I could read it a bit easier. You
would need an outer join if sometimes you didn’t have an area manager.
edit in response to comment :-
The problem of double values is caused by the fact that the area_geo table can have ( and in the case of area_id 3 appears to have ) duplicate values for an area_id. When you do a join a row is returned for each row in the table, so for area_id 3 rows would be returned every time. You could do use a sub query
But if there are different values of Areamanager_name for the same area_id, this query will fail. Ideally I think you need to use a more suitable table to retrieve the areamanager_name, or if there isn’t one normalize your database so that there is only 1 record in area_geo for each area_id.