Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Temp table SUM invalid column error

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.Oid

Group by Clinical.Name,clinical_1.name

INSERT INTO #CounselorCounts ([Name],CounselorCount)

SELECT AgencyName,
SUM(CounselorCount)AS AgencyCount

FROM #CounselorCounts

GROUP BY AgencyName

DROP TABLE #CounselorCounts

error:

Msg 207, Level 16, State 1, Line 17
Invalid 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.Oid

Group by Clinical.Name,clinical_1.name

INSERT INTO #CounselorCounts ([AgencyName],CounselorCount)

SELECT AgencyName,
SUM(CounselorCount)AS AgencyCount

FROM #CounselorCounts

GROUP BY AgencyName

DROP TABLE #CounselorCounts

error:

Msg 207, Level 16, State 1, Line 17
Invalid column name 'Name'.



this should fix it up

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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 below


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.Oid

Group by Clinical.Name,clinical_1.name

can you explain?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.
Go to Top of Page

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.name


wont Clinical.Name alone be enough?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

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?
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

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.
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

JayRU
Starting Member

5 Posts

Posted - 2012-02-06 : 15:42:24
Thank you for that clarification. It is what I need.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-06 : 16:01:04
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -