0

我需要加入来自多个数据库的数据(在同一台服务器上)。这本身似乎并不难。但是,我有一个 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,但无论我尝试什么,我都无法让它工作。

真诚地感谢任何指导。

提前致谢,

马特

4

1 回答 1

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
于 2013-08-19T13:37:59.697 回答