Simple average for a set of records

Hi,


I collect a set of samples for the same product. He wanted to get the simple mean, grouping it by the code column. Returning the query to the entire set of fields.

Attached excel sheet with example

best regards
Example1.xlsx
rflorencioAsked:
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.

Jan LouwerensSoftware EngineerCommented:
SELECT Code, AVG(Value) FROM <Table>
GROUP BY Code

Open in new window

0
Jan LouwerensSoftware EngineerCommented:
Oh, I didn't understand your example at first, but now I got it. Just add the extra fields.

SELECT Id, Code, Avg(Value) as Value, Name, Co, Gr, PA FROM <Table>
GROUP BY Id, Code, Name, Co, Gr, PA

Open in new window

0
Mark WillsTopic AdvisorCommented:
Since you are on SQL2012, you can use windowed functions in aggregations.

Interesting sort sequence, so we will also use row_number()

And because we want to materialise that data (so we can use it), we will use a CTE (Common Table Expression), or, could use a subquery to produce a derived table.

;with cte_avg as
(
   select id, code, [value], [name], co, gr, pa
          , AVG([value]) over (partition by code, [name] order by id desc) as avg_value
          , row_number() over (partition by code, [name] order by id asc) as rn
   from yourdata                                                                                                                 -- change the data source accordingly
)  select id, code, round(avg_value,2) as [Value], [name],co, gr, pa
   from cte_avg 
   where rn = 1

Open in new window




EDIT: missed [name] in final select
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mark WillsTopic AdvisorCommented:
Hmmm... Not sure I understood the requirement.

>>  grouping it by the code column

And looking at your Spreadsheet, the results from my query are identical (apart from missing [name]) to the "What i need: Get Average" with the exception of colour where I get the more accurate 2.25

If I run Jan's code, there is no difference in [value] or avg([value]) given the spreadsheet.

In re-reading the question, maybe this is what you were after :
;with cte_avg as
(
   select id, code, [value], [name], co, gr, pa
          , AVG([value]) over (partition by code, [name]) as avg_value
   from yourdata
)  select id, code, round(avg_value,2) as [Value], [name], co, gr, pa  
   from cte_avg 

Open in new window

I truly dont understand and would greatly appreciate an explanation.
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
rflorencioAuthor Commented:
Hi,

By the tests that I did it seemed good, but at the moment I noticed that I did not consider the column "id". Entering this column does not give the desired result in fact.

best regards
0
Mark WillsTopic AdvisorCommented:
would you like further assistance ?
0
Jan LouwerensSoftware EngineerCommented:
If my answer was not correct for what you're asking for, I don't mind it getting unaccepted.
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
SQL

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.