| Author |
Topic |
|
JayRU
Starting Member
5 Posts |
Posted - 2012-02-06 : 13:55:12
|
| I am attempting to get a sum for a column I counted before placing in a temp table. I am getting a error message telling me one of th columns is invalid which I don't understand. I am linking one table twice in one table and have given one an alias. Any assistance would be greatly appreciated. Thanks in advance. Below is code and error message.Create Table #CounselorCounts(AgencyName nvarchar (50),CounselorCount int)SELECT clinical.name AS [Name], COUNT(Clinical.Name) AS CounselorCount FROM ProviderProvider_ClinicalProviderCounselor INNER JOIN Clinical ON ProviderProvider_ClinicalProviderCounselor.ProviderCounselor = Clinical.Oid INNER JOIN Clinical AS Clinical_1 ON ProviderProvider_ClinicalProviderCounselor.Provider = Clinical_1.OidGroup by Clinical.Name,clinical_1.nameINSERT INTO #CounselorCounts ([Name],CounselorCount)SELECT AgencyName, SUM(CounselorCount)AS AgencyCountFROM #CounselorCountsGROUP BY AgencyNameDROP TABLE #CounselorCountserror:Msg 207, Level 16, State 1, Line 17Invalid column name 'Name'. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-06 : 14:02:13
|
quote: Originally posted by JayRU I am attempting to get a sum for a column I counted before placing in a temp table. I am getting a error message telling me one of th columns is invalid which I don't understand. I am linking one table twice in one table and have given one an alias. Any assistance would be greatly appreciated. Thanks in advance. Below is code and error message.Create Table #CounselorCounts(AgencyName nvarchar (50),CounselorCount int)SELECT clinical.name AS [Name], COUNT(Clinical.Name) AS CounselorCount FROM ProviderProvider_ClinicalProviderCounselor INNER JOIN Clinical ON ProviderProvider_ClinicalProviderCounselor.ProviderCounselor = Clinical.Oid INNER JOIN Clinical AS Clinical_1 ON ProviderProvider_ClinicalProviderCounselor.Provider = Clinical_1.OidGroup by Clinical.Name,clinical_1.nameINSERT INTO #CounselorCounts ([AgencyName],CounselorCount)SELECT AgencyName, SUM(CounselorCount)AS AgencyCountFROM #CounselorCountsGROUP BY AgencyNameDROP TABLE #CounselorCountserror:Msg 207, Level 16, State 1, Line 17Invalid column name 'Name'.
this should fix it up------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-06 : 14:06:40
|
also i didnt understand what you're trying to achieve with belowSELECT clinical.name AS [Name],COUNT(Clinical.Name) AS CounselorCountFROM ProviderProvider_ClinicalProviderCounselor INNER JOIN Clinical ON ProviderProvider_ClinicalProviderCounselor.ProviderCounselor = Clinical.Oid INNER JOIN Clinical AS Clinical_1 ON ProviderProvider_ClinicalProviderCounselor.Provider = Clinical_1.OidGroup by Clinical.Name,clinical_1.name can you explain?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JayRU
Starting Member
5 Posts |
Posted - 2012-02-06 : 14:49:02
|
| I need to provide a count by agency name of counselors in each agency, counselor name is a nvarchar field so I am counting names and grouping by agency name hoping to sum by agency name in the tesmnp table. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-06 : 14:55:28
|
quote: Originally posted by JayRU I need to provide a count by agency name of counselors in each agency, counselor name is a nvarchar field so I am counting names and grouping by agency name hoping to sum by agency name in the tesmnp table.
then why you need to include both of them in group by?Clinical.Name,clinical_1.namewont Clinical.Name alone be enough?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JayRU
Starting Member
5 Posts |
Posted - 2012-02-06 : 15:17:12
|
| I am not sure, but will try that. Your correction count by the counselor name and not by agency. I am not getting anything inserted into the temp table. Any reason why? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-06 : 15:25:00
|
quote: Originally posted by JayRU I am not sure, but will try that. Your correction count by the counselor name and not by agency. I am not getting anything inserted into the temp table. Any reason why?
which correction?i corrected only columnname------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JayRU
Starting Member
5 Posts |
Posted - 2012-02-06 : 15:29:25
|
| The second join of the clinical (clinical_1) table was providing the agency name and what I was using to get the agency name. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-06 : 15:33:16
|
quote: Originally posted by JayRU The second join of the clinical (clinical_1) table was providing the agency name and what I was using to get the agency name.
my question was not on join i was asking about GROUP BY------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
JayRU
Starting Member
5 Posts |
Posted - 2012-02-06 : 15:42:24
|
| Thank you for that clarification. It is what I need. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-06 : 16:01:04
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|