2

我有三个表:AccountManagementManagementAccountLookup

Account 表包含位置。Management 表包含业务层次结构中每个级别的记录。Management 表还包含一个值 ( LevelID) ,该值指示记录位于层次结构中的哪个级别。 ManagementAccountLookup 表是连接它们的查找表。

我在编写查询以获取所有帐户及其两个相关管理记录时遇到问题。

例如:一个 Account 可能有 5 条或更多条与其关联的管理记录,但我只关心 levelID 为 Brand 或 Region 的两个特定管理。另外,我只希望一个帐户在结果网格中显示一次。

结果集应如下所示:

AccountID   Brand      Region
---------   --------   ------
account1    Wendys     East US
account2    McDonalds  West US

这似乎是一个简单的问题,但我无法确切地弄清楚如何获得该结果。我已经尝试过自联接、子查询以及我能想到的所有其他方法,但我似乎无法将结果放在一行中。

任何帮助,将不胜感激。

*编辑:ManagementAccountLookup 有两个字段(AccountID、ManagementID)。这些是另外两个表的 PK。
管理层有一列 LevelID,您可以通过它来判断记录是品牌、地区、地区等...

Brand 和 Region 将是 Management 表中的两个单独的行。我需要结果网格将它们放在同一行中。

4

1 回答 1

2

您需要添加两次 ManagementAccountLookup/Management 组合才能在一行中获取信息。如果需要,我已将LevelID标准直接放在 join 中,以减轻可能过渡到 left-joins 的难度。

select Account.AccountID,
       m_brand.Name Brand,
       m_region.Name Region
  from Account
 inner join ManagementAccountLookup mal_brand
    on Account.AccountID = mal_brand.AccountID
 inner join Management m_brand
    on mal_brand.ManagementID = m_brand.ManagementID
   and m_brand.LevelID = @Insert_Management_Brand_Level_Here
 inner join ManagementAccountLookup mal_region
    on Account.AccountID = mal_region.AccountID
 inner join Management m_region
    on mal_region.ManagementID = m_region.ManagementID
   and m_region.LevelID = @Insert_Management_Region_Level_Here

编辑:如果您需要显示所有帐户,您可以在括号中使用左/内连接组合:

select Account.AccountID,
       m_brand.Name Brand,
       m_region.Name Region
  from Account
  left join 
  (
       ManagementAccountLookup mal_brand
       inner join Management m_brand
         on mal_brand.ManagementID = m_brand.ManagementID
        and m_brand.LevelID = @Insert_Management_Brand_Level_Here
 )
   on Account.AccountID = mal_brand.AccountID
 left join 
 (
       ManagementAccountLookup mal_region
       inner join Management m_region
          on mal_region.ManagementID = m_region.ManagementID
         and m_region.LevelID = @Insert_Management_Region_Level_Here
 )
    on Account.AccountID = mal_region.AccountID

为了使其更具可读性,您可以使用 CTE:

; with mal_level as (
  select AccountID,
         m.LevelID,
         m.Name
    from ManagementAccountLookup mal
   inner join Management m
      on mal.ManagementID = m.ManagementID
)
select Account.AccountID,
       m_brand.Name Brand,
       m_region.Name Region
  from Account
  left join mal_level m_brand
    on Account.AccountID = m_brand.AccountID
   and m_brand.LevelID = @Insert_Management_Brand_Level_Here
  left join mal_level m_region
    on Account.AccountID = m_region.AccountID
   and m_region.LevelID = @Insert_Management_Region_Level_Here

外部应用

select Account.AccountID,
       b.Brand,
       r.Region
  from Account
 outer apply
 (
       select m_brand.Name Brand
         from ManagementAccountLookup mal_brand
        inner join Management m_brand
           on mal_brand.ManagementID = m_brand.ManagementID
          and m_brand.LevelID = @Insert_Management_Brand_Level_Here
        where mal_brand.AccountID = Account.AccountID
 ) b
 outer apply
 (
       select m_region.Name Region
         from ManagementAccountLookup mal_region
        inner join Management m_region
           on mal_region.ManagementID = m_region.ManagementID
          and m_region.LevelID = @Insert_Management_Region_Level_Here
        where mal_region.AccountID = Account.AccountID
 ) r
于 2012-05-24T01:30:45.403 回答