2014-04-01 105 views
1

表1:丟失記錄在SQL Server 2008R2

Date   PlacementID  CampaignID Impressions 
04/01/2014  100    10   1000 
04/01/2014  101    10   1500 
04/01/2014  100    11   500 

表2:

Date   PlacementID  CampaignID Cost 
04/01/2014  100    10   5000 
04/01/2014  101    10   6000 
04/01/2014  100    11   7000 
04/01/2014  103    10   8000 

當我加入使用全部加入和左連接聲明此表,我不能以獲得不常見的記錄,這是table2中顯示PlacementID 103和CampaignID 10和Cost 8000的最後一行。但是我已經搜索了所有原始數據和文件,但是這兩個數據源之間缺少記錄是不常見的。但是,我希望將這些記錄包含在決賽桌中。我怎樣才能做到這一點?這兩個表是兩個不同的來源,我得到的結果只有常見的記錄。

此外,當我發現缺失值是精確值,這是最終數字所要求的,所以要包括每一件事情。我包括我下面的SQL腳本:

SELECT A.palcementid, 
     A.campaignid, 
     A.date, 
     Sum(A.impressions) AS Impressions, 
     Sum(CASE 
      WHEN C.placement_count > 1 THEN (B.cost/C.placement_count) 
      ELSE B.cost 
      END)   AS Cost 
FROM table1 A 
     FULL JOIN table2 B 
       ON A.placementid = B.placementid 
       AND A.campaignid = B.campaignid 
       AND A.date = B.date 
     LEFT JOIN (SELECT Count(A.placementid) AS Placement_Count, 
         placementid. campaignid, 
         date 
        FROM table1 
        GROUP BY placementid, 
          campaignid, 
          date) c 
       ON A.placementid = C.placementid 
       AND A.campaignid = C.campaignid 
       AND A.date = C.date 
GROUP BY A.placementid, 
      A.campaignid, 
      A.date 

我除以安置成本,因爲在源成本被分配只有一個位置和一個時間,所以我必須分配的,因爲在實際的表一樣Placementid重複多比同日期的1倍多。

+0

你好。如果你想談談爲什麼[你最近的問題](https://stackoverflow.com/questions/44422870/convert-transfer-multiple-value-into-columns/44424157)被關閉爲太寬(然後意外刪除),請平息我,我會盡量說明理由。我在其他一些問題中加入了我廣泛的建議,那就是很高興看到你接受了你的一些答案。讓我知道你是否需要幫助。 – halfer

回答

2

由於您沒有提供任何期望的輸出我猜在這裏,但如果你想要的結果是這樣的:

PlacementID CampaignID Date     Impressions Cost 
----------- ----------- ----------------------- ----------- ----------- 
100   10   2014-04-01 02:00:00.000 1000  5000 
100   11   2014-04-01 02:00:00.000 500   7000 
101   10   2014-04-01 02:00:00.000 1500  6000 
103   10   2014-04-01 02:00:00.000 NULL  8000 

然後下面的查詢應該這樣做:

SELECT COALESCE(A.PlacementID,b.placementid) AS PlacementID, 
     COALESCE(A.campaignid, b.campaignid) AS CampaignID, 
     COALESCE(A.date, b.date) AS [Date], 
     SUM(A.impressions) AS Impressions, 
     SUM(CASE 
      WHEN C.placement_count > 1 THEN (B.cost/C.placement_count) 
      ELSE B.cost 
      END) AS Cost 
FROM table1 A 
     FULL JOIN table2 B 
       ON A.[PlacementID] = B.placementid 
       AND A.campaignid = B.campaignid 
       AND A.date = B.date 
     LEFT JOIN (SELECT COUNT(PlacementID) AS Placement_Count, 
         placementid, campaignid, 
         date 
        FROM table1 
        GROUP BY placementid, 
          campaignid, 
          date) c 
       ON A.[PlacementID] = C.placementid 
       AND A.campaignid = C.campaignid 
       AND A.date = C.date 
GROUP BY COALESCE(A.PlacementID, B.PlacementID), 
      COALESCE(A.campaignid, b.campaignid), 
      COALESCE(A.date, b.date) 

樣品SQL Fiddle

+0

jpw:是的,我希望獲得與您在表格中顯示的結果相同的結果,但是當我使用COALESCE嘗試解決方案時,所需的結果未顯示。我之前得到了同樣的結果。 – ADBI

+0

@ user3299024我確實改變了COALESCE旁邊的其他東西;看看這個SQL小提琴:http://www.sqlfiddle.com/#!3/cc480e/12 – jpw

+0

@ user3299024:我很抱歉,先生,但我沒有看到除了COALESCE之外的任何變化,因爲之前做過的jpw。但是,謝謝你們。能否請你幫我解決這個問題,因爲它很緊急。 – ADBI