2012-05-24 78 views
2

我有三個表格:Account,ManagementManagementAccountLookup多次加入表格

該帳戶表包含位置。管理表包含業務層次中每個級別的記錄。管理表還包含一個值(LevelID),指示記錄所屬的層次結構中的哪個級別。ManagementAccountLookup表是連接它們的查找表。

我無法編寫查詢來獲取與其關聯的兩個管理記錄的所有帳戶。

例如:一個賬戶可能有5個或更多與之相關的管理記錄,但我只關心兩個具有品牌或地區級別ID的特定管理。此外,我只想要一個帳戶在結果網格中顯示一次。

結果集應該是這個樣子:

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

這似乎是一個簡單的問題,但我一直無法弄清楚究竟是如何得到這一結果。我嘗試了自連接,子查詢以及其他所有我能想到的內容,但我似乎無法將結果合併到一行中。

任何幫助,將不勝感激。

*編輯: ManagementAccountLookup有兩個字段(AccountID,ManagementID)。那些是另外兩張桌子的PK。
管理有一欄LevelID這是你可以告訴我們,如果記錄是一個品牌,區域,地區,等等

品牌和地區將在管理表中兩個獨立的行。我需要結果網格將它們放在同一行。

+2

也許你可以向我們展示表的模式。我們想要幫助你,但是我們不知道要加入什麼領域 –

+0

對不起,我應該這樣做,我只是不想讓我的問題更加混亂,因爲增加了一些不必要的數據。我做了一個編輯,使它更容易理解表結構。感謝您的意見,我會牢記未來的問題。 – Justin

回答

2

您需要添加ManagementAccountLookup/Management組合兩次以獲取一行信息。我已經將LevelID條件直接放入連接中,以便在需要時可以過渡到左連接。

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 

或者outer apply

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 
+0

太棒了!這看起來像在工作。明天我會做更多的測試來確保,但我認爲這是我需要的。此外,感謝猜測表結構,以提供非常複製粘貼的代碼供我測試。我曾嘗試類似這樣的事情,但我已經加入ManagementAccountLookup表一次,並從那裏兩次加入管理表。那是我被掛斷的地方。 – Justin

+0

我對此聲明有另一個疑問。如果我需要允許帳戶出現在網格中,即使他們沒有區域或品牌關聯,我該如何處理?我不能只改變連接到左連接,因爲這會產生大量重複。我提出的唯一方法(不太好)是將結果聯合到另一個查詢中,而不用管理所有帳戶。有什麼建議麼? – Justin

+0

@Justin對不起,我昨天離開了。請檢查我更新的答案。 –