Solved

Query syntax

Posted on 2014-10-14
3
81 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
[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 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 48

Assisted Solution

by:Dale Fye
Dale Fye 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

Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

636 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