Ok, I’m doing a self query to suppress duplicates for a view. The code I’m using is:
USE BILLING
SELECT Provider_Code,
Provider_LName,
Provider_Fname,
Provider_Title,
AGENCY_LOCATION_NAME_LINE_1,
CostCenter_AbbrName
FROM dbo.ServiceProfiler prov1
INNER JOIN
dbo.ServiceProfiler prov2
ON prov1.Provider_Code = prov2.Provider_Code
WHERE 0 = (SELECT COUNT(s1.Provider_Code)
FROM prov1
WHERE prov2.Provider_Code = prov1.Provider_Code
AND prov2.Provider_Code < prov1.Provider_Code);
When executing the query on SQL Server 2005 I get the message:
Msg 208, Level 16, State 1, Line 3
Invalid object name ‘prov1’.
I cannot for the life of me understand why the alias names are invalid. I think I found a similar question here. But it’s very generic and doesn’t show a full ‘real’ query using the aliases.
I’d do something more like this:
You could also do a straight up sub-select, I’d imagine. I think joining on a subquery is easier to understand, but that’s me.