简体   繁体   English

SQL:复杂问题:连接两个不同的数据库

[英]SQL: Complex Issue: Joining two distinct databases

I need to join data from multiple databases (on the same server). 我需要从多个数据库(在同一服务器上)连接数据。 That in itself doesn't seem to be inherently difficult. 这本身似乎并不固有地困难。 However, I have a CTE that iterates down to get all the children of a specified group. 但是,我有一个CTE可以迭代以获取指定组的所有子级。 Below I have two distinct queries. 下面我有两个不同的查询。 The first one has the proper lineage and the second one gets the proper data. 第一个具有适当的血统,第二个具有适当的数据。 What I don't understand is how to combine these two using the lineage of the first query. 我不明白的是如何使用第一个查询的沿袭将两者结合起来。

Query 1 查询1

This is the lineage I need (where f_locationID = some ID passed from the web app). 这是我需要的沿袭(其中f_locationID =从Web应用程序传递的一些ID)。 This works as desired and gives me all the machines that belong to the location or machines that belong to children of the location. 这可以按需工作,并为我提供了属于该位置的所有计算机或属于该位置的子级的计算机。 However, the data I need for these machines resides in another database entirely. 但是,这些机器所需的数据完全位于另一个数据库中。

;with cte_assets as (
select a.f_locationid, a.f_locationparent, a.f_locationname, 0 as [lev], convert(varchar(30), '0_' + convert(varchar(10), f_locationid)) lineage
from [db_assets].[dbo].[tb_locations] a where f_locationID = '130' UNION ALL
select a.f_locationid
      ,a.f_locationparent
      ,a.f_locationname
      ,c.[lev] + 1
      ,convert(varchar(30), lineage + '_' + convert(varchar(10), a.f_locationid))
from   cte_assets c
join   [db_assets].[dbo].[tb_locations] a
      on a.f_locationparent = c.f_locationID
 )
 select f_assetnetbiosname as 'Computer Name'
 from cte_assets c
 JOIN tb_assets ass on ass.f_assetlocation = c.f_locationID
 ORDER BY f_assetname DESC

Query 2 查询2

This is the data I need. 这是我需要的数据。 The CTE here can be discarded as the lineage was set up merely as an example, and is not what I need. 此处的CTE可以丢弃,因为沿袭仅是作为示例设置的,不是我所需要的。 See the results window below to see the data. 请参阅下面的结果窗口以查看数据。 However, I'd like that data back out for the lineage from above (where 'Computer Name' from query 2 = 'Computer Name' from query 1). 但是,我希望从上方返回该数据以沿袭(其中查询2中的“计算机名称” =查询1中的“计算机名称”)。

;with rCTE as (
   select a.f_itemid
          ,a.f_itemparentid
          ,a.f_itemtype
          ,a.f_itemname
          ,0 as [lev]
          ,convert(varchar(30), '0_' + convert(varchar(10), f_itemid)) lineage
   from [db_reports].[dbo].[tb_locationsmachines] a
   where f_itemid = '1308' 
   UNION ALL
   select a.f_itemid
          ,a.f_itemparentid
          ,a.f_itemtype
          ,a.f_itemname
          ,c.[lev] + 1
          ,convert(varchar(30), lineage + '_' + convert(varchar(10),   a.f_itemid))
from   rCTE c
join   [db_reports].[dbo].[tb_locationsmachines] a
 on a.f_itemparentid = c.f_itemid
)
--below is what I need, not the lineage above
SELECT f_computername as 'Computer Name', 
 SUM(f_sessionlength) AS 'Total Session Time', 
 COUNT(*) as 'Sessions', 
 COUNT(*)/60 as 'Average Sessions per Day', 
 CAST(SUM(f_sessionlength) / 3600E / COUNT(*) as DECIMAL(18,2)) as 'Average Session Length (Hours)'
from rCTE c
JOIN [db_reports].[dbo].[tb_sessions] ss on ss.f_computername = c.f_itemname
GROUP BY f_computername
Order By 'Sessions' DESC, f_computername ASC

The common field between the two tables is 'Computer Name'. 两个表之间的公共字段是“计算机名称”。 I think I need a CTE inside a CTE, but no matter what I try I cannot get it to work. 我认为我需要CTE内部的CTE,但是无论我怎样尝试,都无法使其正常工作。

Any guidance is greatly appreciated, sincerely. 衷心感谢任何指导。

Thanks in advance, 提前致谢,

Matt 马特

The solution is as follows: 解决方法如下:

;with cte_assets as (
select a.f_locationid, a.f_locationparent, 0 as [lev], convert(varchar(30), '0_' + convert    (varchar(10), f_locationid)) lineage
from [db_assets].[dbo].[tb_locations] a where f_locationID = '7' UNION ALL
select a.f_locationid
      ,a.f_locationparent
      ,c.[lev] + 1
      ,convert(varchar(30), lineage + '_' + convert(varchar(10), a.f_locationid))
from   cte_assets c
join   [db_assets].[dbo].[tb_locations] a
      on a.f_locationparent = c.f_locationID
),
cte_a AS 
(
select f_assetnetbiosname as 'Computer Name'
from cte_assets c
JOIN [db_assets].[dbo].[tb_assets] ass on ass.f_assetlocation = c.f_locationID
)

SELECT [Computer Name] as 'Computer Name', 
  SUM(f_sessionlength) AS 'Total Session Time', 
  COUNT(*) as 'Sessions', 
  COUNT(*)/60 as 'Average Sessions per Day', 
  CAST(SUM(f_sessionlength) / 3600E / COUNT(*) as DECIMAL(18,2)) as 'Average Session Length (Hours)'
from cte_a c
JOIN [db_reports].[dbo].[tb_sessions] ss on ss.f_computername = c.[Computer Name]
GROUP BY [Computer Name]
Order By [Computer Name] ASC, 'Sessions' DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM