2017-02-22 54 views
1

我試圖從一個數據庫插入數據到另一個數據庫。這是我到目前爲止,在選擇方面:TSQL - INSERT中的多個值(因爲連接)

USE [db2] 
SELECT 
    sP.pers_FirstName 
    ,sp.pers_LastName 
    ,sPH.Phon_Number 
    ,CASE WHEN LEFT(sPH.Phon_Number, 2) = '04' THEN sPH.Phon_number ELSE NULL END 
    ,CASE WHEN sp.pers_gender = 1 THEN 'M' WHEN sp.pers_gender = 2 THEN 'F' ELSE 'U' END 
    ,CASE 
     WHEN sP.pers_salutation = '10'  THEN 8 
     WHEN sp.pers_salutation = '6'  THEN 2 
     WHEN sp.pers_salutation = '7'  THEN 1 
     WHEN sp.pers_salutation = '8'  THEN 4 
     WHEN sp.pers_salutation = '9'  THEN 5 
     WHEN sp.pers_salutation = 'APROF' THEN 6 
     WHEN sp.pers_salutation = 'Ms.'  THEN 4 
     WHEN sp.pers_salutation = 'PROF' THEN 6 
    END 
    ,sp.pers_dob 
    ,sp.pers_CreatedDate 
    ,sp.pers_UpdatedDate 
    ,'Candidate' 
    ,1 
    ,e.Emai_EmailAddress 
    ,sP.pers_personID 
FROM [db1].dbo.person sP 
LEFT JOIN [db1].dbo.PhoneLink sPL ON sp.pers_personID = sPL.PLink_recordID 
LEFT JOIN [db1].dbo.Phone sPH ON sPL.PLink_PhoneId = sPH.Phon_PhoneID 
LEFT JOIN [db1].dbo.EmailLink eL ON sP.pers_personID = eL.ELink_RecordID 
LEFT JOIN [db1].dbo.Email e ON eL.Elink_EmailID = e.Emai_EmailID 
WHERE 
    (
     sP.pers_employedby NOT IN (
      'Aspen' 
      ,'ACH' 
      ) 
     ) 
    OR 
     (
     sP.pers_employedby IN (
      'Aspen' 
      ,'ACH' 
      ) 
     AND sP.pers_personID NOT IN ( 
      SELECT c.oppo_PrimaryPersonID FROM [SageCRM].dbo.Opportunity c 
      WHERE (c.oppo_contractcompleted <= '2016-01-01' OR c.oppo_contractterminated <= '2016-01-01') and c.Oppo_Deleted is null) 
    AND 
     sp.pers_isanemployee != 'ECHO' 
     AND sP.pers_personID IN (
      SELECT c.oppo_PrimaryPersonID FROM [SageCRM].dbo.Opportunity c 
      WHERE c.oppo_Status != 'In Progress' OR c.oppo_Status = 'Completed') 
     AND sP.pers_dod IS NULL 
     AND sP.pers_FirstName NOT LIKE '%test%' 
     AND sP.pers_LastName NOT LIKE '%test%' 
     AND sp.pers_isanemployee != 'SalesContact' 
) 

由於這樣的事實,每個人的記錄可以鏈接到他們多個電話號碼,我結束了多個記錄,每個人,這顯然不會工作,因爲當我實際插入數據時,我將以重複結束。

問題是,我需要有每個記錄的所有電話號碼,只顯示在不同的領域(家庭電話,工作電話,手機)。

任何想法,除了在每個電話/電子郵件鏈接的單獨插入語句中這樣做?

--------編輯:------------------------------------ -----------------------------

好,所以,我的壞不給你足夠的信息。你的答案都是很好的答案,所以謝謝你(@Horaciux,@John Wu)。

但是,沒有phoneType列,只是一個電話號碼。話雖如此,由於每部手機都是以04以及其他任何家用手機開始的,所以我可以很容易區分這兩種手機類型。

雖然在手機表中有重複,所以我將不得不刪除這些,很可能通過CTE,不應該太難。

所以,我最終會與像這樣的兩個電話號碼:

SELECT (phon_number FROM phone p INNER JOIN PhoneLink p1 on p1.PhoneLinkID = p.PhoneLink WHERE LEFT(p.Phon_Number, 2) = '04') 
SELECT (phon_number FROM phone p INNER JOIN PhoneLink p1 on p1.PhoneLinkID = p.PhoneLink WHERE LEFT(p.Phon_Number, 2) != '04') 

我重複拆卸將是這樣的:

WITH CTE AS 
(
    SELECT phon_linkID, phon_phonNumber, ROW_NUMBER() OVER (PARTITION BY phon_phonNumber ORDER BY phon_linkID) AS RN 
    FROM phone 
) 
DELETE FROM CTE WHERE RN<>1 
+0

所有其他表(家庭電話/工作電話..)? – ydoow

+0

將表結構添加到您的帖子。如果你關心性能,那麼可能是另一種方式。 – Horaciux

回答

2

兩個簡單的步驟。

  1. 擺脫加入電話號碼錶。

  2. 通過在select子句中使用子查詢來查找每條記錄的電話號碼,每種類型的電話都使用一個子查詢。例如

    SELECT sP.pers_FirstName, 
         sP.pers_LastName, 
         (SELECT Phon_Number FROM Phone p JOIN PhoneLink pl ON pl.PhoneLinkID = p.PhoneLinkID WHERE pl.Person_ID = sP.pers_personID AND pl.Type = 'WORK') WorkPhone, 
         (SELECT Phon_Number FROM Phone p JOIN PhoneLink pl ON pl.PhoneLinkID = p.PhoneLinkID WHERE pl.Person_ID = sP.pers_personID AND pl.Type = 'HOME') HomePhone 
    FROM person 
    
2

不知道你的表的結構,我會做一些例子。

select person.id, 
    max(case when phone.type='home' then phone.vlaue else 0 end) 'home', 
    max(case when phone.type='work' then phone.vlaue else 0 end) 'work' 
from person,phone where... 
group by person.id 

然後使用該查詢來加入你怎麼分類的電話號碼名單分成不同的領域需要