2016-04-25 70 views
-1

我需要修改以下sql 2014組查詢以使用來自2個不同服務器的兩個單獨的數據庫。使用1個查詢中的2個不同的sql數據庫

在計算過去7天的平均收入總額之前,我需要首先確定總收入,總成本,&兩個數據庫之間的總毛利。我使用的只是它的偉大作品當前功能不使用收入的總和從兩個database.Tables [DBO]總計[dw_rpt_traffic] & [mediaalpha] [PublisherCallByDay]

我需要加入從表2個不同的server.databases。注意他們應該加入CallDate & CreateDate。但是這兩個表都不包含過去90天所需的所有日期(基於當前日期運行90天),因此這將影響所需的連接類型。

修改後的代碼:仍然有代碼的最後部分的問題,以合併來自兩個數據源的數據以確定平均值。錯誤:無效的對象名稱'rpt'。

WITH RPT 
     AS (SELECT x.CreateDate 
        , x.RevenueTotals 
        , (x.RevenueTotals-x.COSTTOTALS) as GrossProfit 

      FROM  (SELECT CAST(t.Create_DTG AS DATE) AS CreateDate 
          , SUM([AGENT_REV]+[ANCHOR_REV]+[CORP_REV]+[OFFSITE_REV]) as RevenueTotals 
          , SUM([MEDIA_EST_COST]+[OTHER_COST]) as COSTTOTALS 
         FROM  sqlclus3.[abc1234RPT].[dbo].[dw_rpt_traffic] t, 
         WHERE  CAST(t.CREATE_DTG AS DATE) > CAST(GETDATE() - 90 AS DATE) 
         GROUP BY CAST(t.CREATE_DTG AS DATE) 
        ) x 
     ) 
SELECT r.CreateDate 
     ,r.RevenueTotals 
     ,r.GrossProfit 
FROM RPT r 
WHERE r.CreateDate > CAST(GETDATE() - 90 AS DATE) 
ORDER BY r.CreateDate desc; 


WITH Calls 
     AS (SELECT x.Call_Date 
        , x.RevenueTotals 
        , x.Gross_Profit 
      FROM  (SELECT CAST(t.[CallDate] AS DATE) AS Call_Date 
            , SUM(Revenue) as RevenueTotals 
            , SUM(Cost) as CostTotals 
            , SUM(GROSSPROFIT) AS Gross_Profit 
           FROM  [abc123ETL].[mediaalpha].[PublisherCallByDay] t 
           WHERE  CAST(t.[CallDate] AS DATE) > CAST(GETDATE() - 90 AS DATE) 
           GROUP BY CAST(t.[CallDate] AS DATE) 

        ) x 
     ) 
SELECT c.Call_Date 
     ,c.RevenueTotals 
     ,c.Gross_Profit 
FROM Calls c 
WHERE c.Call_Date > CAST(GETDATE() - 90 AS DATE) 
ORDER BY c.Call_Date desc;  

select A.Create_Date 
    , A.RevTotal as RevenueTotal 
    , A.Gross_profit as GrossProfit 
    , AVG(A.RevTotal) OVER (ORDER BY A.Create_Date 
          ROWS BETWEEN 6 PRECEDING AND CURRENT ROW 
          ) AVG7DAYS 
FROM 
    (
    Select Rpt.CreateDate as Create_Date 
     , sum(rpt.RevenueTotals+calls.RevenueTotals) as RevTotal 
     , sum(rpt.GrossProfit+calls.Gross_Profit) as GrossProfits 
    from rpt FULL OUTER JOIN calls 
      on rpt.createDate = calls.call_date 
    ) A 
ORDER BY a.Create_Date 

舊代碼

use sqlclus3.ABC342 

go 



WITH cte 
    AS (SELECT x.CreateDate 
       , x.RevenueTotals 
       , x.RevenueTotals-x.COSTTOTALS as GrossProfit 
       , AVG(x.RevenueTotals) OVER (ORDER BY x.CreateDate 
             ROWS BETWEEN 6 PRECEDING AND  CURRENT ROW 
       ) AS Avg7Days 
     FROM  (SELECT CAST(t.Create_DTG AS DATE) AS CreateDate 
         , SUM([AGENT_REV]+[ANCHOR_REV]+[CORP_REV]+[OFFSITE_REV]) as RevenueTotals 
         , SUM([MEDIA_EST_COST]+[OTHER_COST]) as COSTTOTALS 
        FROM  [dbo].[dw_rpt_traffic] t 
        WHERE  CAST(t.CREATE_DTG AS DATE) > CAST(GETDATE() - 90 AS DATE) 
        GROUP BY CAST(t.CREATE_DTG AS DATE) 
       ) x 
    ) 

SELECT c.CreateDate 
    ,c.RevenueTotals 
    ,c.Avg7Days 
    ,c.GrossProfit 

FROM cte c 

WHERE c.CreateDate > CAST(GETDATE() - 90 AS DATE) 

ORDER BY c.CreateDate desc; 


Use SEASQL03.[ABC123] 

go 

WITH Calls 
    AS (SELECT x.Call_Date 
       , x.RevenueTotals 
       , x.Gross_Profit 
       , AVG(x.RevenueTotals) OVER (ORDER BY x.[Call_Date] 
             ROWS BETWEEN 6 PRECEDING AND CURRENT ROW 
       ) AS Avg7Days 
     FROM  (SELECT CAST(t.[CallDate] AS DATE) AS Call_Date 
         , SUM(Revenue) as RevenueTotals 
         , SUM(Cost) as CostTotals 
         , SUM(GROSSPROFIT) AS Gross_Profit 
        FROM  [mediaalpha].[PublisherCallByDay] t 
        WHERE  CAST(t.[CallDate] AS DATE) > CAST(GETDATE() - 90 AS DATE) 
        GROUP BY CAST(t.[CallDate] AS DATE) 
       ) x 
    ) 

SELECT c.Call_Date 
    ,c.RevenueTotals 
    ,c.Avg7Days 
    ,c.Gross_Profit 

FROM Calls c 

WHERE c.Call_Date > CAST(GETDATE() - 90 AS DATE) 

ORDER BY c.Call_Date desc; 

標籤 SQL-serversql服務器2014 編輯總結

+0

它一點也不清楚你想在這裏做什麼。 –

+1

我相信你需要檢查[鏈接服務器](http://stackoverflow.com/questions/4091960/sql-server-linked-server-example-query) – TTeeple

+0

我試過你的建議,但是,它不喜歡select語句第二部分的單引號。 SELECT * FROM OPENQUERY(sqlplus3,SELECT * FROM abs212。[dbo]。[dw_rpt_traffic] –

回答

0

我認爲解決這個最好的方法是在服務器添加鏈接的服務器,所以你可以調用其他數據庫。

Add a linked server

對於這一點,你去一臺服務器,並在出現的根目錄中的文件夾中,有一個「服務器對象」文件夾,一旦進入,展開鏈接服務器的文件夾。您可以通過右鍵單擊該文件夾並選擇「新鏈接服務器」選項來添加新的鏈接服務器。

您必須完成這些信息,例如它是SQL Server實例或其他信息。

最後,在安全選項卡,必須輸入憑據(用戶名和密碼登錄服務器),喜歡它的下一個圖像中的真實所示:

Server Connection

一旦你準備好這一點,您可以通過右鍵單擊來測試新的鏈接服務器,然後選擇「測試連接」。

如果它工作正常,則只需指定服務器和表,這樣調用該數據庫的任何表:

SELECT Column1, Column2 FROM [ServerName].[DatabaseName].[Schema].[TableName] 

在你的情況,應該是這樣

SELECT CAST(t.Create_DTG AS DATE) AS CreateDate 
          , SUM([AGENT_REV]+[ANCHOR_REV]+[CORP_REV]+[OFFSITE_REV]) as RevenueTotals 
          , SUM([MEDIA_EST_COST]+[OTHER_COST]) as COSTTOTALS 
FROM [sqlclus3].[ABC342].[dbo].[dw_rpt_traffic] 
相關問題