aspdotnetuser
Programmer
Hi,
I'm trying to get a small amount of data from lots of tables and I'm confused about what the SQL code should be. Sorry if this question is long, any help will be appreciated
Here are the tables:
BatchedScores, Visits, Branch, Client, ClientRegion, ClientPeriod
Each Client has a number of branches and each branch has multiple visits. Each visit has a total score and the scores are batched and stored in the BatchedScores table.
Can someone help me modify this query to get the average scores as percentages for each region by periodName, (for example, period name might be jan, feb, march and region south, south west etc)
Here is the SQL I have so far:
[blue]DECLARE @ClientUID nvarchar(10)
DECLARE @ClientPeriodUID nvarchar(25)
SET @ClientUID = ''
SET @ClientPeriodUID = ''
SELECT t1.Name, t1.PeriodName
FROM (SELECT ClientRegion.Name, ClientPeriod.PeriodName, Visit.Period, Visit.VisitUID,
INNER JOIN BatchedVisitScores ON Visit.VisitUID = BatchedVisitScores.VisitUID
INNER JOIN ClientPeriod ON Visit.Period = ClientPeriod.ClientPeriodUID
INNER JOIN ClientRegion ON Client.ClientUID = ClientRegion.ClientUID
INNER JOIN Branch ON Client.ClientUID = Branch.BranchUID
WHERE Client.ClientUID = @ClientUID
and BatchedVisitScores.BranchUID = Visit.BranchUID <--- This causes an error
) AS t1
GROUP BY t1.PeriodName, t1.Name[/blue]
Error message: [red]The objects "Visit" and "Visit" in the FROM clause have the same exposed names. Use correlation names to distinguish them.[/red]
I tried using aliases for visit but it didn't work.
I'm trying to get a small amount of data from lots of tables and I'm confused about what the SQL code should be. Sorry if this question is long, any help will be appreciated
Here are the tables:
BatchedScores, Visits, Branch, Client, ClientRegion, ClientPeriod
Each Client has a number of branches and each branch has multiple visits. Each visit has a total score and the scores are batched and stored in the BatchedScores table.
Can someone help me modify this query to get the average scores as percentages for each region by periodName, (for example, period name might be jan, feb, march and region south, south west etc)
Here is the SQL I have so far:
[blue]DECLARE @ClientUID nvarchar(10)
DECLARE @ClientPeriodUID nvarchar(25)
SET @ClientUID = ''
SET @ClientPeriodUID = ''
SELECT t1.Name, t1.PeriodName
FROM (SELECT ClientRegion.Name, ClientPeriod.PeriodName, Visit.Period, Visit.VisitUID,
INNER JOIN BatchedVisitScores ON Visit.VisitUID = BatchedVisitScores.VisitUID
INNER JOIN ClientPeriod ON Visit.Period = ClientPeriod.ClientPeriodUID
INNER JOIN ClientRegion ON Client.ClientUID = ClientRegion.ClientUID
INNER JOIN Branch ON Client.ClientUID = Branch.BranchUID
WHERE Client.ClientUID = @ClientUID
and BatchedVisitScores.BranchUID = Visit.BranchUID <--- This causes an error
) AS t1
GROUP BY t1.PeriodName, t1.Name[/blue]
Error message: [red]The objects "Visit" and "Visit" in the FROM clause have the same exposed names. Use correlation names to distinguish them.[/red]
I tried using aliases for visit but it didn't work.