SQL:复杂问题:加入两个不同的数据库

时间:2013-08-16 16:28:27

标签: sql sql-server-2008 tsql

我需要从多个数据库(在同一台服务器上)加入数据。这本身似乎并不困难。但是,我有一个CTE,它迭代下来以获取指定组的所有子项。下面我有两个不同的查询。第一个具有适当的谱系,第二个获得正确的数据。我不明白的是如何使用第一个查询的谱系组合这两个。

查询1

这是我需要的血统(其中f_locationID =从Web应用程序传递的一些ID)。这可以根据需要工作,并为我提供属于该位置的所有机器或属于该位置的子项的机器。但是,这些机器所需的数据完全存在于另一个数据库中。

;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

查询2

这是我需要的数据。这里的CTE可以被丢弃,因为谱系的建立仅仅是一个例子,而不是我需要的。请参阅下面的结果窗口以查看数据。但是,我希望从上面找回该数据的数据(来自查询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

两个表之间的公共字段是“计算机名”。我想我需要在CTE内部进行CTE,但无论我尝试什么,都无法让它发挥作用。

非常感谢任何指导。

提前致谢,

马特

1 个答案:

答案 0 :(得分:1)

解决方案如下:

;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