我看了很多建议,但在过去两个小时内无法解决如何解决这个问题。
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对象“Personals.Groups”和 FROM子句中的“Personals.Groups”具有相同的公开名称。使用 相关名称来区分它们。
感谢。
答案 0 :(得分: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