SQL query issue

I have a rather straight forward sql query that I need help changing
Here is the current query - MI_PRINX is the primary key - the reason I am using like is that the Plan_Year field is text and there are some non-stand entires
select MI_PRINX, Unit_ID, unit, Plan_Year, Harv_Type,age, TotAc
From HarvestUnits

where Plan_Year like YEAR(GetDate())
order by age

Open in new window


what I need to do is run a query that will bring back the following
1) I need to be able to select the plan year (so I only get entries from that year - I will clean up that column)
2) I want to some the TotAc (acres) based on age)

so thinks would be grouped as all harvest units that are 1 year old in 2015 - this is the sum of their acres
and it would do that for all entries for the selected year..
I hope that makes since
thanks for the help
David ModugnoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NerdsOfTechTechnology ScientistCommented:
SELECT SUM(TotAc), age, Plan_Year
FROM HarvestUnits
WHERE Plan_Year = GetDate()
GROUP BY age

Open in new window

0
David ModugnoAuthor Commented:
I get this error
Column 'HarvestUnits.Plan_Year' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
0
NerdsOfTechTechnology ScientistCommented:
Omit the select column Plan_Year; try,

SELECT age, SUM(TotAc)
FROM HarvestUnits
WHERE Plan_Year = GetDate()
GROUP BY age

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
I don't understand specifically what you're trying to sum up.  Is it just a basic sum?:

select Plan_Year, age, sum(TotAc) as SumTotAc
From HarvestUnits
where Plan_Year like CAST(YEAR(GetDate()) AS varchar(4)) + '%'
group by Plan_Year, age
order by age
0
David ModugnoAuthor Commented:
perfect thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.