Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 当与表联接时,避免每个实例的行重复_Sql_Sql Server 2008_Tsql - Fatal编程技术网

Sql 当与表联接时,避免每个实例的行重复

Sql 当与表联接时,避免每个实例的行重复,sql,sql-server-2008,tsql,Sql,Sql Server 2008,Tsql,嗨,我有3张桌子,我正试图加入他们,以得到一张渴望的桌子。我尝试了group by和temp tables选项以获得所需的表格,但没有任何帮助。我希望避免一个表中的值的每个实例与另一个表中的值重复 表1客户表: CstId CstDetails CstType ---------- --------------- ------------ 1 address 1 1 2 addres

嗨,我有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
但是我让CstGroupNote对所有CstNote进行重复,这是我试图避免的

有什么方法可以达到这个效果吗

下面是我使用的代码:

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
尝试:


使用派生表和外部联接
这里的诀窍是
和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

到目前为止,您的查询是什么?我已经创建了两个临时表,一个用于获取customernotes,另一个用于获取groupnotes,然后使用基于CustomerID的左外部联接联接联接两个临时表。下面是我使用的代码:选择c.cstid、c.cstdetails、cn.cstnotesid,cn.cstnotetxt从客户c插入temp1左侧外部连接客户备注cn在c.cstid=cn.cstid上其中c.customertypeid=1选择cr.cstid,cr.cstgroupid,cn.cstgroupnoteid,cn.cstnotetxt从客户关系cr左侧外部连接客户备注cn在cr.cstgroupid=cn.customerid选择t1.cstid,t1.cstdetails,t1.cstnotesid、t1.cstnotetxt、t2.cstgroupnoteid、t2.cstnotetext来自t1.cstid=t2.cstid上的temp1 t1左外部联接t2您作为示例提供的表缺少列!在查询示例中,它们有更多的列。我们需要它们的数据,以便将其与您提供的所需表相匹配CstNoteId是noteid的别名,cstnotetxt是notetxt的别名cstgroupnoteid和CstGroupNoteTXT的情况相同谢谢,但您的解决方案只有在notesid匹配时才起作用,而在我的情况下,它并非始终为真。感谢您的解决方案,它真的帮助了我。@user2091818:很高兴我能帮上忙。
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