与表连接时,避免为每个实例重复行

时间:2013-02-20 15:53:49

标签: sql sql-server-2008 tsql

嗨我有3张桌子,我正在尝试加入他们以获得欲望表。我尝试过group by和temp tables选项来获取所需的表但没有帮助。我想避免在另一个表的一个表中的每个值的实例重复。

表1客户表:

CstId          CstDetails       CstType    
----------  ---------------  ------------
    1           address 1         1
    2           address 2         1
    3           address 3         1
    4           address 4         2
    5           address 5         2

表2客户关系:

CstId            CstGroupId
----------   ----------------
    1               4 (this is same as CustomerId)
    2               5 (this is same as CustomerId)
    3               4 (this is same as CustomerId)

表3客户备注:

CstId          NotesId     NoteTxt
-----------   ---------    ---------
    1            1           note11
    1            2           note12
    1            3           note13
    3            1           note31
    4            1           note41
    4            2           note42
    4            3           note43
    4            4           note44
    4            5           note45

现在我希望结果采用以下格式

Table result:
                                    (NoteId)  (Notetxt)    (NoteId)         (Notetxt)
    CstId  CstDetails  CstGroupId  CstNoteId   CstNote   CstGroupNoteId   CstGroupNote
      1    address1       4         1          note11        1              note41
      1    address1       4         2          note12        2              note42
      1    address1       4         3          note13        3              note43
      1    address1       4         null       null          4              note44
      1    address1       4         null       null          5              note45

但我正在为所有CstNote重复CstGroupNote,我试图避免这种情况。

有没有办法可以达到这个效果?

以下是我使用的代码:

select c.cstid, c.cstdetails, cn.cstnotesid, cn.cstnotetxt
insert into temp1
from customer c
    left outer join customernotes cn
        on c.cstid = cn.cstid
where c.customertypeid = 1

select cr.cstid, cr.cstgroupid, cn.cstgroupnoteid, cn.cstnotetxt
insert into temp2
from customerrelationship cr
    left outer join customernotes cn
        on cr.cstgroupid = cn.customerid

select t1.cstid, t1.cstdetails, t1.cstnotesid, t1.cstnotetxt, t2.cstgroupnoteid, t2.cstnotetext
from temp1 t1
    left outer join t2
        on t1.cstid = t2.cstid

2 个答案:

答案 0 :(得分:0)

尝试:

select CstId, 
       max(CstDetails) CstDetails, 
       max(CstGroupId) CstGroupId, 
       max(CstNoteId) CstNoteId, 
       max(CstNote) CstNote, 
       max(CstGroupNoteId) CstGroupNoteId, 
       max(CstGroupNote) CstGroupNote
from
(select c.CstId, 
        c.CstDetails,
        0 CstGroupId,
        n.NotesId CmbNotesId,
        n.NotesId CstNoteId,
        n.NoteTxt CstNote,
        0 CstGroupNoteId,
        '' CstGroupNote
 from customer c
 left outer join customernotes n on c.cstid = n.cstid
 where c.customertypeid = 1
 union all
 select c.CstId,
        c.CstDetails,
        r.CstGroupId,
        n.NotesId CmbNotesId,
        0 CstNoteId,
        '' CstNote,
        n.NotesId CstGroupNoteId,
        n.NoteTxt CstGroupNote
 from customer c
 left outer join customerrelationship r on c.cstid = r.cstid
 left outer join customernotes n on r.CstGroupId = n.cstid
 where c.customertypeid = 1) u
group by CstId, CmbNotesId

答案 1 :(得分:0)

使用派生表和外部联接
诀窍是 和cn.cstnotesid = cG.cstnotesid
将这两个链接在一行

select c.cstid, c.cstdetails, cn.cstnotesid, cn.cstnotetxt
      ,cG.CstGroupId, cG.cstnotesid, cG.cstnotetxt
from customer c
join customernotes cn
  on cn.cstid = c.cstid 
outer join  (select c.cstid, c.CstGroupId, cn.cstnotesid, cn.cstnotetxt
                from customer c
                join customernotes cn
                on cn.cstid = c.CstGroupId) as cG
        on c.cstid = cG.cstid
       and cn.cstnotesid = cG.cstnotesid
order by c.cstid, cn.cstnotesid, cG.cstnotesid