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
 Column renaming from Query output

Author  Topic 

CoVS
Starting Member

1 Post

Posted - 2010-07-14 : 11:07:08
Apologys - I am VERY new to this SQL nonsense and so this is probably a very silly question!

I want to be able to name a column created from my query.

Query is:

select A.OrigRef, A.DisplayName, A.ExtCode, count(CalcId)
from OrigRefView A, CalcView B where A.OrigRef = B.NewRef and A.OrigRef like 'AB%'
group by A.OrigRef, A.DisplayName, A.ExtCode
order by A.SusRef


and it returns the Count in "column4"
Is there a way I can get the query to output a different column name without creating a whole new table (i.e. not by creating a new table for my query output and then running a new procedure at the end to rename the column...)?

i.e. I want "column4" to read "CountofCalId"

Many thanks!

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-07-14 : 11:11:10
select A.OrigRef, A.DisplayName, A.ExtCode, count(CalcId) as CountOfCalcId
from OrigRefView A, CalcView B where A.OrigRef = B.NewRef and A.OrigRef like 'AB%'
group by A.OrigRef, A.DisplayName, A.ExtCode
order by A.SusRef

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-14 : 11:12:24
select A.OrigRef, A.DisplayName, A.ExtCode, count(CalcId) as CountofCalId
from OrigRefView A, CalcView B where A.OrigRef = B.NewRef and A.OrigRef like 'AB%'
group by A.OrigRef, A.DisplayName, A.ExtCode
order by A.SusRef


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-07-14 : 11:12:54



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2010-07-14 : 11:21:01
Yes, but you took the extra time to put the correction in bold face

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-07-16 : 11:52:36
Note that all aggregates,usage of functions over a column and case expression by default wont produce a column name

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-07-17 : 09:49:19
Also you can't specify a column (A.SusRef) in ORDER BY, unless it is part of the GROUP BY or the aggregate function

select A.OrigRef, A.DisplayName, A.ExtCode, count(CalcId) as CountofCalId
from OrigRefView A, CalcView B where A.OrigRef = B.NewRef and A.OrigRef like 'AB%'
group by A.OrigRef, A.DisplayName, A.ExtCode
order by A.SusRef



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -