2013-02-20 59 views
0

嗨,我有3個表,我想加入他們得到一個願望表。我嘗試了分組和臨時表選項來獲得所需的表,但沒有幫助。我想避免從另一個表中的一個表中的每個值的每個實例重複。當與表加入時,避免重複每個實例的行

表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 
+0

到目前爲止您的查詢是什麼? – dotjoe 2013-02-20 15:56:57

+0

我已創建2個臨時表之一來獲得customernotes和第二得到groupnotes,然後我加入都與左外臨時表聯接根據客戶ID下面 – user2091818 2013-02-20 16:01:01

+0

是我使用的代碼:選擇c.cstid,c.cstdetails,CN。 cstnotesid,cn.cstnotetxt 從客戶C插入temp1中 \t左外連接CN \t \t customernotes上c.cstid = cn.cstid 其中c.customertypeid = 1個 選擇cr.cstid,cr.cstgroupid,CN .cstgroupnoteid,cn.cstnotetxt 從customerrelationship插入到temp2 cr \t左外連接自定義ernotes CN \t \t上cr.cstgroupid = cn.customerid 選擇t1.cstid,t1.cstdetails,t1.cstnotesid,t1.cstnotetxt,t2.cstgroupnoteid,t2.cstnotetext 從T1的temp1 \t左外連接T2 \t \t on t1.cstid = t2.cstid – user2091818 2013-02-20 16:11:56

回答

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 
+0

謝謝你的解決方案同樣的事情,這真的幫了我所需的表。 – user2091818 2013-02-21 23:32:29

+0

@ user2091818:很高興能幫到你。 – 2013-02-22 09:51:59

0

使用派生表和外部聯接
有訣竅的
和cn.cstnotesid =鳥苷.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 
+0

謝謝,但是你的解決方案只有在notesid匹配的情況下才起作用,在我的情況下它不是一直如此。 – user2091818 2013-02-21 23:31:32