Solved

Query syntax

Posted on 2014-10-14
3
76 Views
Last Modified: 2014-10-28
Hello,

Can someone explain the different between the two lines of code marked below in terms of the way the grouping is:


with cte as (
 select 
  -1 as kli_kliniknr,
  max(m.MeasurementDate) as MeasurementDate, 
  SUM(NumberOfMeasurements) as NumberOfMeasurements,
  SUM(NumberOfMen) as NumberOfMen,
  SUM(NumberOfWomen) as NumberOfWomen,

  -- What is the difference between these two rows?
  avg( Q1 * NumberOfMeasurements / NumberOfMeasurements) as Q1,
  sum( Q1 * NumberOfMeasurements) / sum(NumberOfMeasurements) as Q1x,

from [BPSD].[dbo].[MeasurementStatisticsPerUnit] m
group by m.MeasurementDate )
 
select * from cte ;

Open in new window

0
Comment
Question by:soozh
3 Comments
 
LVL 22

Assisted Solution

by:plusone3055
plusone3055 earned 167 total points
ID: 40380750
-- What is the difference between these two rows?
  avg( Q1 * NumberOfMeasurements / NumberOfMeasurements) as Q1,
  sum( Q1 * NumberOfMeasurements) / sum(NumberOfMeasurements) as Q1x,

Row 1 is AVERAGING
Row2 is SUMING

Row1  AVERAGE (Q1 * number of measurements / number of mesaurements
Row2  (its adding all together)  SUM(q1 * numberof measurements / SUM(numberof measurements)
0
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 166 total points
ID: 40380788
The AVG line is simply taking the average of the values of Q1.  The NumberOfMeasurements/Number of Measurements aspect of that element cancel each other out.

The Sum line looks like it is actually taking what most analysts would call the "weighted average".  This is determined by taking the sum of a value times the number of occurances of that value, and then dividing by the total number of measurements (the Sum(NumberOfMeasurements) figure).  So, if your table or query contains data like:

Q1     NumOfMeas
5                2
6                3
7               4

The first line would compute to the average of three elements)
AVG(5*2/2 , 6*3/3, 7*4/4) = 6

But the second method would compute as:
SUM(5*2, 6*3, 7*4)/Sum(2, 3, 4) = (10+18+28)/9 = 56/9 = 6.2222.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 167 total points
ID: 40380924
The first one is a simple average of Q1 ["* NumberOfMeasurements / NumberOfMeasurements" will always cancel each other out]

The second one, as Dale noted, is a different type of average, and could yield results quite different from the simple average.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
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.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

862 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

23 Experts available now in Live!

Get 1:1 Help Now