Solved

Avg in SQL without including all records

Posted on 2016-08-23
14
74 Views
Last Modified: 2016-08-25
Hi, I need a query which computes different rates averages... However, not sure how to do it as the avg will include the omitted records. I have the following query to date:

SELECT Format([checkin],"yyyy mm") AS YearMonth,  Format(Avg(IIF(PCKGID<>1,0,Resvalue/TotNights)),"$#,##0.00") As AllInclPerPax, Format(Avg(IIF(PCKGID<>2,0,Resvalue/TotNights)),"$#,##0.00") As BBPerPax
FROM QueryFull
WHERE cancel=False
GROUP BY Format([checkin],"yyyy mm");

The result is not correct as the avg will include the omitted records. Any help is very much appreciated! Thanks Michael
0
Comment
Question by:mpim
[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
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41767928
I'm not sure I understand your problem.

Omitted records will not be included in your average.  Here is a simple test:

test
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41767929
Here is another example:

 test2
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41767975
How do you know if a record is omitted? Do you have a field to represent that? If so, can you include that in the WHERE clause.

WHERE omitted_field <> 'omitted'
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41768065
@Sharath - from the original post:

WHERE cancel=False

Open in new window

0
 
LVL 22

Expert Comment

by:yo_bee
ID: 41768162
Are you trying to do something like this?

 select year(Transdate)as [year],month(Transdate)as [month],
		avg(Case when stage <> 'billed' then value else null end) as NotBilled,
		avg(Case when stage <> 'wip' then value else null end) as NotWIP
		from Transactions
		where year(transdate) =   '2016'
Group by year(Transdate),month(Transdate)

Open in new window


Please disregard the lack of the formatting
.

year        month       NotBilled                               NotWIP
----------- ----------- --------------------------------------- ---------------------------------------
2016        1           14.955020                               492.523185
2016        2           32.510240                               412.929915
2016        3           56.734543                               542.809811
2016        4           111.553329                              461.521153
2016        5           66.373036                               727.830537
2016        6           96.414381                               461.795339
2016        7           394.863501                              233.488841
2016        8           297.647818                              80.708655
2016        9           2273.720000                             NULL
2016        12          738.090000                              NULL
0
 

Author Comment

by:mpim
ID: 41769371
If I have the following table in Access:
RateType      ResValue
1                      $1,000.00
1                     $2,000.00
1                      $3,000.00
2                     $5,000.00
2                     $10,000.00
2                    $15,000.00

Now in a single Select I would like to compute the AVG for all RateType=1 (which is USD 2000) and the average  for all RateType=2 (which is USD 10.000). However, if I use

SELECT avg(IIf(ratetype=1,resvalue,0)), Avg(Iif(ratetype=2,resvalue,0))
FROM TestTable;

then the result is wrong as it computes the average using all 6 rows. I very much appreciate any help..
Cheers Michael
0
 
LVL 22

Accepted Solution

by:
yo_bee earned 500 total points
ID: 41769394
You need to group using the RateType
Once you remove the group by the statement thinks each row should be aggregated.
Add RateType back in to the select part and group by RateType.

SELECT Table1.RateType, Avg(IIf([Table1]![RateType]=1,[Table1]![ResValue],0)) AS Expr1, Avg(IIf([Table1]![RateType]=2,[Table1]![ResValue],0)) AS AvgOfResValue
FROM Table1
GROUP BY Table1.RateType;

Open in new window



RateType      rate1  Rate 2
1      $2,000.00      $0.00
2      $0.00      $10,000.00
0
 
LVL 41

Expert Comment

by:Sharath
ID: 41769396
In your example, you need to calculate average by grouping similar items.

SELECT RateType, AVG(ResValue) AS Avg_ResValue
  FROM your_table
GROUP BY RateType

Open in new window

0
 
LVL 22

Expert Comment

by:yo_bee
ID: 41769399
Is this a Homework assignment.
It sure seems like one.
0
 

Author Comment

by:mpim
ID: 41769431
No  - it is the same project I'm working on as always... pls don't ask me this qustion again.... :)
0
 
LVL 22

Expert Comment

by:yo_bee
ID: 41769440
NP ;)

What is your over goal here?
Does the look of it make a difference or can you use what Sharath suggested?


Do no need it to look like

Rate1     Rate2
--------      --------
$2000    $10000
0
 

Author Comment

by:mpim
ID: 41769441
Thanks for asking - the solution from Sharath is perfect! I will close in a minute...
0
 

Author Closing Comment

by:mpim
ID: 41770704
Perfect ... Thank you Michael
0
 
LVL 22

Expert Comment

by:yo_bee
ID: 41770936
@MPIM

You awarded me all the points, but mentioned that Sharath suggestion was perfect.
I would request to have this question reopened so you can award the point accordingly.

At least a 50/50 split.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

756 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