2011-10-16 53 views
4

我看了很多建議,不知道如何在過去的兩個小時內解決這個問題。TSQL Msg 1013「使用相關名稱來區分它們。」

SET DATEFORMAT DMY 

DECLARE @Source DATETIME = '01/01/2001' 
DECLARE @Destenaition DATETIME = '01/01/2020' 

SELECT ST.[Group], 
     ST.Shop, 
     SUM(ST.Purchased) AS Total, 
     CHG.Charged 
FROM (SELECT Personals.Groups.[Name]  AS 'Group', 
       Cards.vPurchases.PersonalID, 
       Personals.Registry.[Name], 
       SUM(Cards.vPurchases.Ammont) AS Purchased, 
       Cards.vPurchases.ShopName AS Shop 
     FROM Cards.vPurchases 
       INNER JOIN Personals.Registry 
       ON Personals.Registry.Id = Cards.vPurchases.PersonalID 
       INNER JOIN Personals.Groups 
       ON Personals.Registry.[Group] = Personals.Groups.Id 
       INNER JOIN Personals.Groups 
       ON Personals.Groups.Id = CHG.GroupID 
     WHERE Cards.vPurchases.[TimeStamp] >= @Source 
       AND Cards.vPurchases.[TimeStamp] <= @Destenaition 
     GROUP BY Cards.vPurchases.PersonalID, 
        Personals.Registry.[Name], 
        Personals.Groups.[Name], 
        Cards.vPurchases.ShopName) ST, 
     (SELECT PG.Id      AS GroupID, 
       SUM(Cards.vCharges.Amount) AS Charged 
     FROM Cards.vCharges 
       INNER JOIN Personals.Registry 
       ON Personals.Registry.Id = Cards.vCharges.PersonalID 
       INNER JOIN Personals.Groups AS PG 
       ON Personals.Registry.[Group] = PG.Id 
     WHERE Cards.vCharges.[TimeStamp] >= @Source 
       AND Cards.vCharges.[TimeStamp] <= @Destenaition 
     GROUP BY Personals.Groups.[Name]) AS CHG 
GROUP BY ST.Shop, 
      ST.[Group] 

然後我得到這個錯誤:

Msg 1013, Level 16, State 1, Line 6 The objects "Personals.Groups" and "Personals.Groups" in the FROM clause have the same exposed names. Use correlation names to distinguish them.

感謝。

回答

10

您在第一個子查詢中使用了兩次表Personals.Groups。 如果你真的想要表Personals.Groups你需要給他們一個別名,然後使用,而不是在查詢的其餘部分的表名稱。

INNER JOIN Personals.Groups as PG1 

INNER JOIN Personals.Groups as PG2 

如果你只需要一個就可以結合on條款只使用一個替代。

INNER JOIN Personals.Groups 
    ON Personals.Registry.[Group] = Personals.Groups.Id and 
    Personals.Groups.Id = CHG.GroupID 
+0

謝謝你的解決方案! – SamekaTV

相關問題