Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • 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 JacksonCommented:
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 JacksonCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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