2015-02-11 71 views
0

在SQL Server 2012中編寫查詢以加入這兩個視圖 - 活動和服務。 考慮到這個條件以3個字段爲條件加入2個視圖

所有的響應跟蹤將需要在BP,手機和電子郵件ID級別完成。

如果BP爲空,則爲移動號碼或emailid 或者如果移動號碼爲空,則採用BP或emailid 或者如果電子郵件ID爲空,則採用移動號碼或BP。

將所有3個參數放在一起&不管其他2個字段是否填滿,3個鍵中的每一個都將與相應的服務報告進行比較,最終結果是3個鍵之間的結果的或。

我開始這樣,但它是不完整的,沒有道理。

select * from [dbo].[Campaign] left outer join 
       [dbo].[Services] on [Campaign].[Email]=[Services].[Email] 
where [dbo].[Campaign].[Email] is not null and [dbo].[Campaign].[Mobile]is not null 

截圖顯示的兩種觀點各領域

enter image description here

感謝, 馬尼什

+0

不要在這裏添加屏幕截圖,嘗試輸入所有需要的數據。 – 2015-02-11 11:05:02

+0

它是列,而不是字段... – jarlh 2015-02-11 11:07:08

+0

很難理解你在說什麼?你想檢查的第二個組合的情況下,第三是空的那部分我得到,但之後,你說的是不可理解的。你能更清楚一點嗎? – 2015-02-11 11:18:21

回答

0

你可以嘗試這樣的,(試圖瞭解背後的想法,這樣就可以準確地修改根據您的需要

SELECT * 
FROM [dbo].[Campaign] 
LEFT OUTER JOIN [dbo].[Services] ON (
     ([Campaign].[Email] = [Services].[Email]) 
     OR ([Campaign].[Mobile] = [Services].[Mobile]) 
     ) 
WHERE [dbo].[Campaign].[bp] IS NULL 

UNION ALL 

SELECT * 
FROM [dbo].[Campaign] 
LEFT OUTER JOIN [dbo].[Services] ON (
     ([Campaign].[Email] = [Services].[Email]) 
     OR ([Campaign].[bp] = [Services].[bp]) 
     ) 
WHERE [dbo].[Campaign].[Mobile] IS NULL 

UNION ALL 

SELECT * 
FROM [dbo].[Campaign] 
LEFT OUTER JOIN [dbo].[Services] ON (
     ([Campaign].[Mobile] = [Services].[Mobile]) 
     OR ([Campaign].[bp] = [Services].[bp]) 
     ) 
WHERE [dbo].[Campaign].Email] IS NULL 
0

請嘗試像這樣

SELECT * 
FROM [dbo].[Campaign] c 
     LEFT OUTER JOIN [dbo].[Services] s 
        ON CASE 
         WHEN c.bp IS NULL 
           AND (c.email = s.email 
            OR c.mobile = s.mobile) THEN 1 
         WHEN c.Mobile IS NULL 
           AND (c.email = s.email 
            OR c.bp = s.bp) THEN 1 
         WHEN c.email IS NULL 
           AND (c.bp = s.bp 
            OR c.mobile = s.mobile) THEN 1 
         ELSE 0 
         END = 1 
WHERE [dbo].[Campaign].[Email] IS NOT NULL 
     AND [dbo].[Campaign].[Mobile]IS NOT NULL