在1个查询中使用2个不同的sql数据库

时间:2016-04-25 16:20:23

标签: sql-server sql-server-2014

我需要修改以下sql 2014查询集,以便从2个不同的服务器使用两个独立的数据库。

我需要首先确定总收入,总费用和&在根据收入总额的总和计算过去7天的平均值之前,2个数据库之间的总毛利。我正在使用的当前函数工作得很好,除了它没有使用来自两个数据库的收入总和的总和。表[dbo]。[dw_rpt_traffic]& [mediaalpha]。[PublisherCallByDay]

我需要从2个不同的server.databases加入表。请注意,它们应该由CallDate& amp; 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 编辑摘要

1 个答案:

答案 0 :(得分:0)

我认为解决此问题的最佳方法是在服务器中添加链接服务器,以便您可以调用其他数据库。

Add a linked server

为此,您转到其中一个服务器,并在根目录中显示的文件夹中,有一个“Server Objects”文件夹,一旦进入,就展开Linked servers文件夹。在那里,您可以通过右键单击此文件夹并选择“新建链接服务器”选项来添加新的链接服务器。

您必须完成信息,例如,如果它是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]