?
Solved

query - averages with different criteria

Posted on 2014-02-24
3
Medium Priority
?
362 Views
Last Modified: 2014-02-24
Dear experts -
I have a workhorse table of expense items. Each item has a person, an action code, rate paid, language, etc.

Now, I need to generate a relatively long list of AVERAGES, by LANGUAGE - for example, for action 'x' where another field = y, for action 'z' where another field has a different value, etc.

Is there some way to do those other than creating a separate query for each average needed?

The point is - it would be easy if the criteria for the underlying dataset were identical (e.g., do an average of field x, then field y, and then field z where all the records have the following criteria...). But that's not the case.

Can I use some sort of IIF or other construct that would allow me to do this?

Thanks!
0
Comment
Question by:terpsichore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 39882393
Hi terp,

Can you give us a bit more detail about what you're trying to do?  Perhaps an example?

And are you looking for a solution that runs in MS Access or on another engine.  The SQL can be quite different.


Kent
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 2000 total points
ID: 39882476
You could use SUM / COUNT, where you use a case-statement to specify criteria (note: I used NULLIF to replace zero with null, so you don't divide by zero; you may add more and/or in the case-statement), example:
select	avg1 = SUM(case when cond1 = 1 then value end) / NULLIF(COUNT( case when cond1 = 1 value end),0)
,	avg2 = SUM(case when cond2 = 2 then value end) / NULLIF(COUNT( case when cond2 = 2 value end),0)
from	workhorse 

Open in new window

0
 

Author Closing Comment

by:terpsichore
ID: 39882630
I think this should be perfect. thank you.
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

752 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