Solved

Group by cloumn name

Posted on 2011-02-24
5
444 Views
Last Modified: 2012-05-11
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
Comment
Question by:minglelinch
  • 2
  • 2
5 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 34974881
You can't use a column alias in a group by statement unfortunately
0
 
LVL 1

Author Comment

by:minglelinch
ID: 34974903
What should I use? I cannot use real column name neither as the result is not I want.
0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 34974961
try : select Count(*) as 'Amount', datepart(year, incidentdate) as 'Incdate' from MyTable group by datepart(year, incidentdate)
0
 
LVL 1

Author Comment

by:minglelinch
ID: 34975003
Great ! That's the answer. Thanks a lot !
0
 
LVL 4

Expert Comment

by:kelle1
ID: 34975011
Change it to this

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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question