我需要修改以下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 編輯總結
它一點也不清楚你想在這裏做什麼。 –
我相信你需要檢查[鏈接服務器](http://stackoverflow.com/questions/4091960/sql-server-linked-server-example-query) – TTeeple
我試過你的建議,但是,它不喜歡select語句第二部分的單引號。 SELECT * FROM OPENQUERY(sqlplus3,SELECT * FROM abs212。[dbo]。[dw_rpt_traffic] –