• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

Group by cloumn name

I wrote the following query, and I got error - Invalid column name 'Incdate'.
select Count(*) as 'Amount', datepart(year, incidentdate) as 'Incdate' from MyTable group by Incdate

I changed it to Group by incidentdate, but the reult is not what I want.

What name should I use for the group by?  I need to get the sum amount for individule year.
Thanks.
0
minglelinch
Asked:
minglelinch
  • 2
  • 2
1 Solution
 
Paul JacksonSoftware EngineerCommented:
You can't use a column alias in a group by statement unfortunately
0
 
minglelinchAuthor Commented:
What should I use? I cannot use real column name neither as the result is not I want.
0
 
Paul JacksonSoftware EngineerCommented:
try : select Count(*) as 'Amount', datepart(year, incidentdate) as 'Incdate' from MyTable group by datepart(year, incidentdate)
0
 
minglelinchAuthor Commented:
Great ! That's the answer. Thanks a lot !
0
 
kelle1Commented:
Change it to this

SELECT COUNT(*) AS 'Amount', DATEPART(year, incidentdate) AS 'Incdate' FROM wv_timecard GROUP BY DATEPART(year, incidentdate)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now