将查询结果与sql中的另一个结合起来

时间:2012-11-14 18:55:44

标签: sql

下面的以下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)

表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

预期结果

 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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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;

答案 2 :(得分: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;

编辑以回应评论: -

double值的问题是由于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,或者如果您的数据库没有normalize,那么area_geo中每个area_id只有1条记录。