Solved

Groupings And Percentages

Posted on 2014-12-16
4
99 Views
Last Modified: 2014-12-16
Hi,
I have a table containing the following columns:
ProdGroup,   ProductID, PercentFat, PercentProt.

I would like to be able to get the average PercentFat and PercentProt per ProdGroup.
The fields are all of type INT. There are multiple ProductIDs within each ProdGroup.

So to be able to return one row for each ProdGroup, together with the average percentage PercentFat  and PercentProt

Any assistance would be appreciated
Thanks
0
Comment
Question by:Morpheus7
  • 2
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
Comment Utility
Select ProdGroup, AVG(PercentFat) as Avg_PercentFat, AVG(PercentProt) as Avg_PercentProt
From MYTABLE
Group By ProdGroup
Order By ProdGroup

Open in new window


This assumes that you are happy in having the averages as INTs.
0
 

Author Comment

by:Morpheus7
Comment Utility
Many thanks for your reply. If I convert to REAL, would this give me a more accurate figure?
Thanks
0
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
Yes. - Make sure you do the CAST or CONVERT inside the AVG, i.e. AVG(CAST instead of CAST(AVG(
0
 

Author Closing Comment

by:Morpheus7
Comment Utility
Many thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now