1

下面的以下 sql 查询产生了这个结果

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;

结果

 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

我的意图是进一步将 Areamanager_name 包含在下表中的上述结果中。然而,下表对上述结果 (Area) 有一个共同的属性 (Area_ID)

表区_地理

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

预期结果

 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
4

3 回答 3

0

您需要使用LEFT OUTER JOIN. http://en.wikipedia.org/wiki/Join_%28SQL%29

于 2012-11-14T18:58:02.707 回答
0
select cust.*, area.AREA_Nm,area.AREA_Dp
from area inner join cust on area.area_number=customer.area
          inner join Area_Geo on cust.area = Area_Geo.area
ORDER BY customer.Cust_ID;
于 2012-11-14T19:14:28.337 回答
0

我认为下面应该这样做,我重新格式化了一下,这样我可以更容易地阅读它。如果有时您没有区域经理,您将需要外部联接。

SELECT cust.*
      ,area.area_Nm
      ,area.AREA_Dp
      ,area_geo.Areamanager_name
FROM area inner join cust     on area.area_number = customer.area
          inner join area_geo on area.area_number = area_geo.area_id
ORDER BY customer.Cust_ID;

编辑以回应评论: -

双重值的问题是由于 area_geo 表可以具有(并且在 area_id 3 的情况下似乎具有)area_id 的重复值。当您进行连接时,表中的每一行都会返回一行,因此对于 area_id,每次都会返回 3 行。你可以使用子查询

SELECT cust.*
      ,area.area_Nm
      ,area.AREA_Dp
      ,(SELECT DISTINCT area_geo.Areamanager_name
        FROM   area_geo
        where  area.area_number = area_geo.area_id) Areamanager_name
FROM area inner join cust     on area.area_number = customer.area
ORDER BY customer.Cust_ID;

但是如果同一个 area_id 有不同的 Areamanager_name 值,这个查询就会失败。理想情况下,我认为您需要使用更合适的表来检索 areamanager_name,或者如果没有一个规范化您的数据库,以便每个 area_id 在 area_geo 中只有 1 条记录。

于 2012-11-14T19:14:39.870 回答